Error when calculate the difference between two dates

I’m trying to calculate the difference between two dates but I kept getting this error message and couldn’t figure out why. I have been reading from the forum and don’t know what i did wrong. Thank you so much!

image

image

You shouldn’t name a variable DateDiff as DateDiff is a method of the datetime class. Change the name of your variable and see if the error goes away.

Changed it but the error still there

image

@ndtran85

  1. Drag an “Assign” activity onto your workflow.
  2. In the “To” field of the Assign activity, create a new variable to store today’s date (let’s call it today). Set the variable type to DateTime.
  3. In the “Value” field, use the DateTime.Today property to get today’s date.
  4. Drag another “Assign” activity onto your workflow.
  5. In the “To” field of the Assign activity, create a new variable to store the other date (let’s call it otherDate). Set the variable type to DateTime.
  6. In the “Value” field, use the DateTime.ParseExact method to convert the string representation of the other date to a DateTime object. For example:

DateTime.ParseExact(“2024-05-10”, “yyyy-MM-dd”, System.Globalization.CultureInfo.InvariantCulture)

Replace “2024-05-10” with the string representation of your desired date.

  1. Drag another “Assign” activity onto your workflow.
  2. In the “To” field of the Assign activity, create a new variable to store the difference between the two dates (let’s call it dateDifference). Set the variable type to TimeSpan.
  3. In the “Value” field, subtract today from otherDate:

otherDate - today

  1. Now, you can use the dateDifference variable to extract the desired information, such as total days, total hours, etc., using its properties (e.g., dateDifference.TotalDays, dateDifference.TotalHours , etc.).

hi @treesa.maria

it works. Thanks so much!

1 Like

You don’t have to do all that. It’s creating extra variables you don’t need. It can all be done in one expression the way you originally had it.

Looking back at your original post, you are converting DateTime.Now to string and then converting it back to datetime. This is redundant.

Use this as your expression to assign to NumberofDays.

DateDiff(DateInterval.Day,DateTime.ParseExact(date1,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture),Now)

Also if you are only using NumberofDays once (ie in an If condition, entering it into a field, etc) then you don’t need this variable. You can just directly use that expression in the If condition, entering it into the field, etc.

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