How can I compare these two dates


I wanna compare time on this date column to 23:59:01 if time on excel is larger true else false

This is my current code:

datetime.ParseExact(CurrentRow(“Date”).ToString, “hh:mm:ss”, system.Globalization.CultureInfo.InvariantCulture) > DateTime.ParseExact(“23:59:09”, “hh:mm”, system.Globalization.CultureInfo.InvariantCulture)

Hey @joscares

Does this post help?

Cheers

Steve

Hi @joscares

ParseExact should match the correct format, please try the below code for comparing the values

DateTime.ParseExact(“11/21/2022 11:59:10 PM”, “MM/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture)

Format description:

  • 2-digit month
  • 2-digit day-of-month
  • 4-digit year
  • 2-digit hour (12 hour format)
  • 2-digit minutes
  • 2-digit seconds
  • am/pm designator

Will give output : 11/21/2022 23:59:10

DateTime.ParseExact(“23:59:09”, “HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).ToString

Will give output : 02/01/2023 23:59:09

Note: I have used “HH” to represent 24 hour format

Based on the values in your Date column in excel, result for all the records will be false.
because when you don’t specify the date in second half of the code. it will always take current date and the first half is in the past date, so (Past > Current = False)

To compare only time between two dates, use “TimeOfDay” along with above code.

(DateTime.ParseExact(“11/21/2022 11:59:10 PM”, “MM/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture).TimeOfDay > DateTime.ParseExact(“23:59:09”, “HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).TimeOfDay).ToString

image

(DateTime.ParseExact(CurrentRow(“Date”).ToString, “MM/dd/yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture).TimeOfDay > DateTime.ParseExact(“23:59:09”, “HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).TimeOfDay).ToString

Cheers
John

1 Like

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