Comparing dates - earlier/later

Hello, I have extracted a column of date(A) from mail, and then extracted another column of date(B) from a database website. I want to check if (A) comes before or after (B). May I know what is the workflow for such comparison?

I tried using if (A) < (B) as my boolean, but the results are not what I expected as they gave me an output of 23/07/2017 < 20/06/2017 which is not true as 23/07/2017 comes after. They are in string form and I have no idea how to convert them to DateTime.

Please advise.

Here we go :slight_smile: …

5 Likes

When I tried “Date.ParseExact(strDateFrom,“dd, MM, yy”,CultureInfo.InvariantCulture)” I got the compiler error “Culture Info is not declared”.

Your example datetimecomparison.xaml would not load (Error “Activity could not be loaded because of errors in the XAML”).

Please clarify…

AJ

Hi Alan,

Please import System.Globalisation namespace

For this, please open Package Manager and install Workflow Manager Activities

2 Likes

I would be grateful if you could direct me to the user guidance that describes when and where additional namespaces are required and the same for packages.

AJ

I installed the new package and imported the namespace, as per your instructions. But, despite replicating the use of DateTime.ParseExtact exactly as indicated (as far as I can tell), I still get the “CultureInfo not declared” error…

Screenshot included…

AJ

First, make sure your “user_time” variable is of type DateTime. If it’s not, select Browse for types in Variable type and search for System.DateTime.

Second, if you just type it into the Expression editor and click Ok the error will disappear. As far as i know, there are some methods that are not recognized by UiPath intellisense.

In the Activities guide you will see that each activity has the corresponding package. For example, for Read Range we have UiPath.Excel.Activities.ExcelReadRange.

As for the activities in Workflow Manager Activities, you can find more information here:

For example, if you google CultureInfo, you will find more info like namespace and assembly here:

@b4bbler
Hi, not sure you got this answered yet so thought I’d point out that you can use the below:
System.Globalization.CultureInfo.InvariantCulture in place for the CultureInfo error you got.

1 Like

My equivalent to your “user_time” variable is of type DateTime.

That works with my current workflow, “Cultureinfo.InvariantCulture” doesn’t.

Is this the best way of converting date strings, as it all seems so hugely and unnecessarily complex for such a fairly generic requirement? “System.Convert.ToDateTime(strDateFrom)” seems to work in some circumstances, without importing “System,Globalization”. I don’t follow why it doesn’t always work…

Cdate(string) or Convert.ToDateTime(string) are equal. But as mentioned elsewhere in this forum, the negatives from using that method is that it assumes the date format is always Month/day/year in that order so “dd/MM/yyyy” will give wrong results.

I typically use Cdate(“9/1/2017”).ToString(“MM/dd/yyyy”) for formatting (for example) and that works fine, but Parse is probably more reliable in the long run for handling dates internationally.

Just my 2 cents

4 Likes

It seems complex and doesn’t always work because it is complex. To a computer, “9/1/2017” is just a sequence of characters with no inherent meaning, in fact they are just stored as bytes. To a human it might be 9 January or 1 September; the computer does not know which interpretation is correct, so you need to tell it in some way. Also, a human reader will not even pause to recognise “9/1/2017”, “09/01/2017” and “9 January 2017” as exactly the same day in his particular culture, but purely factually they are quite different strings.

So there are more and less precise ways to do this, of which Convert.ToDateTime is the least fussy: it works on Object variables (such DataTable cells) as well as Strings and will not fail on variables that are null, returning DateTime.MinValue, which is 01/01/0001 0:00.

Internally this method calls DateTime.Parse on the string representation of its input. The linked page probably has the most elaborate information on DateTime parsing. Most importantly, Parse may not recognise the format of a date unless you additionally pass it a CultureInfo argument, e.g. New CultureInfo("en-GB") for UK English. Note also that Parse will convert even minimal info like “9.1” and fill in the (current) year and a time of 0:00.

Perhaps the most significant other method is DateTime.ParseExact, which can take an additional string or string array containing format specifications that will be tried to parse the date string. These can look like “MM/dd/yyyy”, where MM, dd and yyyy are only three out of dozens of possibilities.

Unfortunately I cannot make this sound easy as it is not an easy topic. In short, if the dates you are handling happen to be parseable using the default culture (which is the “invariant culture”, another wonderfully confusing term), you’re in luck; if not, you will have to provide the right CultureInfo or the right format string(s). However, please do check out the “Workflow Manager Activities” package, which has several activities pertaining to dates.

2 Likes