Date/Time Difference between to String Variables

Hi Team - I need to calculate difference between two string variables saving Date & Time values.

str1 = 09/29/2023 15:12:52
str2 = 09/29/2023 15:23:41

(str2-str1) =?

I need to find the difference between these string variables and output should be in seconds str format.
One more scenario is some times the dates diff may span multiple days too. For above case
str3: 09/30/2023 15:23:41
(str3-str1) = ?

Hi @KrishnaKishore

str1 = "09/29/2023 15:12:52"
str2 = "09/29/2023 15:23:41"
str3 = "09/30/2023 15:23:41"
datetime1= DateTime.ParseExact(str1, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
datetime2 = DateTime.ParseExact(str2, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
datetime3 = DateTime.ParseExact(str3, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)
timeDifference1= (datetime2 - datetime1).TotalSeconds
timeDifference2= (datetime3 - datetime1).TotalSeconds

Note: str1 , str2, str3 are of DataType System.String. datetime1, datetime2, datetime3 are of DataType System.DateTime. timeDifference1, timeDifference2 is of DataType System.Double

Hope it helps!!

Hi @KrishnaKishore

First convert the string to datetime variables

dateTime1 = DateTime.ParseExact(str1, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture)
dateTime2 = DateTime.ParseExact(str2, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture)
dateTime3 = DateTime.ParseExact(str3, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture)

Then find the difference(Output is double type)

timeDifference1 = (dateTime2 - dateTime1).TotalSeconds
timeDifference2 = (dateTime3 - dateTime1).TotalSeconds
1 Like

Hi @KrishnaKishore

Can you try with this expression in the assign activity

date1 = DateTime.ParseExact(str1, "MM/dd/yyyy HH:mm:ss", system.Globalization.CultureInfo.InvariantCulture)
date2 = DateTime.ParseExact(str2, "MM/dd/yyyy HH:mm:ss", system.Globalization.CultureInfo.InvariantCulture)

Calculate the time difference between date1 and date2

timeDifference = date2 - date1

Regards
Gokul

1 Like

Hi, you can create workflow as follows

(datetime2.Subtract(datetime1)).ToString(“hh:mm:ss”)

1 Like

Hi @KrishnaKishore

Check below video for your reference

Hope this may help you

Thanks,
Srini

1 Like

Thanks @Parvathy , It worked.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.