Unable to compare date


#1

Hello,

I am trying to compare two dates. One is the date extracted from mail through Outlook which is of the type string and the other one is the System’s date and the type is DateTime. In order to compare these two I am using Compare Date feature which expects variable in Date form. How do I go about. Please suggest some idea.

Thanks in advance!!
Tamshi


Compare two dates
Get a date in formart "dd/mm/yyyy"
Reading CSV and Writing to Excel changes datatype
#2

Hey @tamshi

DateTime supports comparison, but first you need to parse the date-time string, DateTime.ParseExact() should.

var dateTimeStr = “16-03-2017”;
var user_time =DateTime.ParseExact(datetimestr, “dd-MM-yyyy”, CultureInfo.InvariantCulture); // parse
var time_now = DateTime.Now.Date; // to get today’s date

now use compare date activity on it.

For assistance see the attached sample workflow.
datetimecomparison.xaml (6.3 KB)

Regards…!!
Aksh


Comparing dates - earlier/later
#3

Thanks @aksh1yadav for the help. I tried this in my workflow but I am getting an error. I have attached the screenshot of that. Also if you could explain about CultureInfo.InvariantCulture, it would be of great help.

Regards
Tamshi


#4

Hey @tamshi

You are getting this problem because you have not imported the namespace “System.Globalization”.

once you will import it, you will not get that error.

Not all cultures use the same format for dates and decimal / currency values.

This will matter for you when you are converting input values (read) that are stored as strings to DateTime, float, double or decimal. It will also matter if you try to format the aforementioned data types to strings (write) for display or storage.

In the Windows operating system the user may even customize how numbers and date/times are formatted and may also choose another culture than the culture of his operating system. The formatting used is the choice of the user which is how it should be.

So when you format a value to be displayed to the user using for instance ToString or String.Format or parsed from a string using DateTime.Parse or Decimal.Parse the default is to use the CultureInfo.CurrentCulture. This allows the user to control the formatting.

However, a lot of string formatting and parsing is actually not strings exchanged between the application and the user but between the application and some data format (e.g. an XML or CSV file). In that case you don’t want to use CultureInfo.CurrentCulture because if formatting and parsing is done with different cultures it can break. In that case you want to use CultureInfo.InvariantCulture (which is based on the en-US culture). This ensures that the values can roundtrip without problems

Regards…!!
Aksh


#5

Thanks @aksh1yadav for the information.


#6

Are you sure about this?
I’m pretty certain that for calls made within UiPath the default culture is invariant, unless you specifically provide a different one.
Sample output when running on pl-PL user settings and en-US interface:

UiPath:

C#:

Same will happen with Decimal.Parse etc.

Unless by default you ment good practice, in which case I agree (in most cases - monitoring systems are IMHO better left at UTC or one specific timezone).

Regards,
Andrzej


Usage of date or time functions does not consider local settings
#7

I was not aware that Uipath using the default culture as variant and mentioned because i used to follow this.

And It is good thing about CultureInfo.InvariantCulture that It is associated with the English language(‘en-US’) but not with a country or region.

and that is why mentioned as well:

But anyhow now knows that internally they are using it. :slight_smile:

Regards…!!
Aksh[quote=“andrzej.kniola, post:6, topic:2392”]
better left at UTC or one specific timezone
[/quote]

Agree :+1:

InvariantCulture is better for internal usage, when you e.g saves a date to a text-file or parses data. And a specified CultureInfo is better when you present data to the end user.

Regards…!!
Aksh