Calculating the time difference between dates when the variables are string

can use help:

  1. calculating the time difference between dates (“dd/MM/yyyy”)- in days
  2. calculating the time difference between dates(“dd/MM/yyyy hh:mm:ss”) -in hours/minutes/seconds

Hi @Lior_Ben_Naim

(Date.ParseExact(endDate, "dd/MM/yyyy", Nothing)-Date.ParseExact(startDate, "dd/MM/yyyy", Nothing)).Days

Here startDate and endDate are string variable holding date in dd/MM/yyyy format

(DateTime.ParseExact(endDate, "dd/MM/yyyy hh:mm:ss", Nothing)-DateTime.ParseExact(startDate, "dd/MM/yyyy hh:mm:ss", Nothing)).Duration

should the variables be with ending like:
.Tostring?
.ToString(“dd/MM/yyyy hh:mm:ss”)?

if the one of the variable is System.DateTime.Now.ToString(“dd/MM/yyyy hh:mm:ss”)

For the output you can use .ToString

an error rises:

(DateTime.ParseExact(System.DateTime.Now, “dd/MM/yyyy hh:mm:ss”, Nothing)-DateTime.ParseExact(Process_Start_Time_Stamp, “dd/MM/yyyy hh:mm:ss”, Nothing)).Duration.tostring

@Lior_Ben_Naim

What is the data type of Process_Start_Time_Stamp? Check in variable panel
Also, For System.DateTime.Now there is no need to use DateTime.ParseExact
Also, Check what is the output if you type System.DateTime.Now in log message activity.

Simply use this

(Now-DateTime.ParseExact(Process_Start_Time_Stamp, "dd/MM/yyyy hh:mm:ss", Nothing)).Duration.ToString
1 Like

wow thank you!
you gave me a lot of good advice :slight_smile:

how can i change the output format? (it took close to 15 seconds and the output is:
“12:00:14.7405741”,

@Lior_Ben_Naim

it will give you seconds

Math.Ceiling(Cdbl("12:00:14.7405741".Split(":"c).Last))

For the whole string

Join(durationString.Split(":"c).Select(Function (x) Math.Round(CDbl(x),0).ToString).ToArray, ":")

Or better use this

System.Text.RegularExpressions.Regex.Replace(durationString, "\d+\.\d+$", Math.Round(CDbl(durationString.Split(":"c).Last), 0).ToString)

Here durationString = "12:00:14.7405741", i.e., the difference output

the 14.7405741 is fine by me but not the 12:00 before that, is there a solution?

What is the output you want?

because it is showing you the correct difference between two dates.
here 12 is for hours

00:00:15

@Lior_Ben_Naim

The difference between the two dates is not 15 seconds that’s why 12 is appearing. It is hh:mm:ss format.

you are right, but i am running this from Now to 15 second ago,why is the 12 is showing?

What is the code you are using?
Send the screenshot of your workflow

thank you for your help:

There is no need to convert Now to a String if you want to calculate the difference.
Simply use like this

I have changed the workflow. Please check it.

Date Diff.xaml (5.1 KB)

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