I am having time in two variable. Want to add both variable

I am having time in two variable. Want to add both variable. Using below function but getting an error. How to fix.
DateTime.Parse(startTime).Add(DateTime.Parse(timeDifference)).ToString

@Puneet_Singh1 Can you show both variables value and desire output

use “+” instead add
and change the type of your variable

@Puneet_Singh1

Please try this

(DateTime.Parse(startTime)+DateTime.Parse(timeDifference)).ToString

cheers

You’re trying to DateTime.Parse timespans, which won’t work, and DateTime.Parse returns a datetime which you’re trying to assign to finalEndTime which is also a timespan.

If StartTime is a datetime or a timespan variable, and timeDifference is a timespan variable, just add them:

StartTime + timeDifference

Both variable value are 9:30 and 1:00:00
I want to be add both value in HH:mm

Are they strings or timespans? If they’re timespans just add them:

StartTime + timeDifference

If they’re strings, parse them into timespans and add them:

TimeSpan.Parse(“9:30”) + TimeSpan.Parse(“1:00:00”)

image

image

Hi
if value are 20:36 and 3:30:00

@Puneet_Singh1
If your variables are string type then try this one

NewStrVar = TimeSpan.parse(v1).add(TimeSpan.parse(V2)). ToString

Output is incorrect.

Again, 20:36 and 3:30:00 are both timespans. You don’t use datetime.parse on them.

Are they STRING variables or are they TIMESPAN variables? You still haven’t answered this question.

If they’re timespan, just add them

startTime + endTime

If they’re string, TimeSpan.Parse them and add them

TimeSpan.Parse(startTime) + TimeSpan.Parse(endTime)

Both are string

@Puneet_Singh1
Try this one resultTime = (DateTime.ParseExact(time1, "hh:mm tt", CultureInfo.InvariantCulture) + DateTime.ParseExact(time2, "hh:mm tt", CultureInfo.InvariantCulture)).ToString("hh:mm tt")

Then do this:

TimeSpan.Parse(startTime) + TimeSpan.Parse(endTime)

Everyone thinks you have to DateTime.Parse it but you don’t. A TimeSpan is literally the same as the HH:mm values in a datetime, as measured from midnight.

Nothing working i am stuck over here. Getting error timespan cannot converted to string although

DateTime.Parse(startTime).Subtract(DateTime.Parse(timeDifference)).ToString this is working. DOnt know the reason.

when i am putting add in place of substract it shows an error

You don’t replace TimeSpan with your variable name.

Literally type TimeSpan.Parse() and put your variable in the parentheses. And you keep using DateTime.Parse. It’s supposed to be TimeSpan.Parse