Date Compare Issue

Hi Team,

I am extracting a date in format - Jan, 05, 2022 10:10 AM

I want the above date to be compared with the system/current date and result should
be which date is greater

@Robotics please follow these steps
1-use assign activity and left side put your date variable.
2-put right side ->Cdate(DateVariable).ToString(“dd-MM-yyyy hh:mm:ss”)
3-use another assign activity and put current Date variable
4-right side put this one->System.DateTime.Now.ToString(“dd-MM-yyyy hh:mm:ss”)
5-compare both

Main.xaml (7.0 KB)

Hi Raja,

Attached the file, facing some issues. Need your help

@Robotics ok. I am looking it. please give me few mints

Sure, thanks :slight_smile:

Hello @Robotics ,
Kindly check this Xaml file
DateCompare.xaml (12.8 KB)

  1. The value convert into date format from string
DateTime.ParseExact(DateString,"MMM, dd, yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture)
  1. Finally condition for if condition
DateTime.Now>DateConvert

This is wrong. You don’t compare dates as strings.

Hi @Robotics,

kindly check attached Xmal

Datetime.ParseExact(“Jan, 05, 2022 10:10 AM”,“MMM, dd, yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture) > Now

Date.xaml (5.6 KB)

Thanks,
RajKumar

1 Like

Why would you eliminate the AM or PM? They’re an important part of the datetime.

You have to convert this to a datetime value using ParseExact. This method takes a format string so it knows how to parse the string.

DateTime.ParseExact(“Jan, 05, 2022 10:10 AM”,“MMM, dd, yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture)

Then you can compare that datetime value to Now:

image

1 Like

@postwick Because he asked to compare the date in greayer or lesser in date only, not in time. So I eliminated it.
Now I updated it. AM or PM the code will works

I doubt he was intentionally saying date to mean only compare the date portion. But simply eliminating the AM/PM from the string still wouldn’t accomplish that. You don’t compare dates as strings, you have to convert the string (including AM/PM) to a datetime and then compare.

@postwick Yeah Got it :love_you_gesture:

Hi Rajkumar,

Attached almost worked but got error when i changed the time format
from 06: 10PM to 14:10 PM - changed this in first assign activity. Attached the changed assisgn xaml as well.
Error: String was not recognized as a valid date time
Suggestions needed
Date.xaml (5.9 KB)

If you change the format of the string, you have to change the format you give to ParseExact.

And 14:10 PM is wrong. 14:10 is 24-hour format so you don’t use AM/PM. There is no reason to change it. Just do this. This is the correct solution and works as tested.

Its invalid time format.
while AM PM apperas, time will not in 24 hrs format. Try in 12 Hours format
DateCompare.xaml (12.8 KB)
For 12 hrs Format(with am/pm)

DateTime.ParseExact(DateString,"MMM, dd, yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture)

For 24 hrs format

DateTime.ParseExact(DateString,"MMM, dd, yyyy HH:mm",System.Globalization.CultureInfo.InvariantCulture)

If you need to eliminate AM or PM Join the 2nd assign it will works
image

Regards,
Gokul Jai

There is absolutely no reason to remove the AM/PM.

14:10 and 6:10PM are literally exactly the same values in a datetime.

Hi,

I am using the same code what you said still getting the same err. Below is the code

In Assign
Application_Date(String dt type) = “Sep, 09, 2022 14:44 PM”

If
DateTime.ParseExact(Application_Date,“MMM, dd, yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture) > Now

In Application_Date i need the result irrespective we have 11:00 or 12:00 or 14:00 as well.
Its works well until time changed between 01:00 to 12:00. From/above time 13:00 its getting error. If: String was not recognized as a valid DateTime.

when taking out the time part we can do:

1 Like

@ppr Great,
Thanks for the knowledge sharing.