Date difference and format

Hi, I need to find diffrence between two date time variables in HH:mm:ss format.
I am doing (d2- d1).ToString(“HH:mm:ss”)
Is this correct?

Thank you,

Hi @A_Learner

That is technically correct, however you should validate that the second date, that is, the one you are going to compare against, is always greater than the initial date, since if it is less you may have problems.


1 Like

Hey @A_Learner
you can try this: (2.4 KB)

Hello @A_Learner

The difference between two dates should be stored in a Timespan variables.


Hi @A_Learner

Please check the below thread:

You can try below way for example:

startDate= DateTime.Now.AddDays(-1)
endDate= DateTime.Now.AddHours(1)
timeDifference= (endDate - startDate).ToString("hh\:mm\:ss")

startDate, endDate is of DataType System.DateTime. timeDifference is of DataType System.String.

If you have any specific date formats mention that format, I will help you out with the syntax.


1 Like