Date formats /logic

Hi,

Need suggestions to improve a potential error coming in Date conversion. In most of the cases when the date is provided in “MM/dd/yy” format - the “yy” is getting converted to any year that is being formed with some logics.

In my case, if the given date is like “02/15/13” or “02/18/56” - it is taking 02/15/2013 (which may be or may not be right. coz the DOB may be by 1913 as well). But, in the second case (02/18/56) - it should be for the previous year (02/18/1956) - where as Convert.ToDateTime(“MM/dd/yyyy”) takes the value as “02/18/2056” (which is future) - not correct.

As DOB may not occur in future case - how to make it take with only the less than current year? Anyone faced this issue? Solutions?

1 Like

You could try setting your CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax property to the current year. In this way, 2 digits years would be convert up to 2022, but a 23, for instance, would become 1923.
You’d have to research how to manipulate your CultureInfo in UiPath (search how to do it in C# and try replicating it in UiPath with Invoke Code or Assign activities), and the use it in your Convert.ToDateTime call.

However, this does not solve the issue of years equal or smaller than 22, where you will have not way of knowing where if it is 1922 or 2022.
This is a tricky one, maybe you if have the age of the person or other info you would be able to ge through it.

2 Likes

See if something alng this lines work, I haven’t tested it:

CultureInfo culture = new CultureInfo(“en-US”);
culture.Calendar.TwoDigitYearMax = 2022;
string dateString = “02/18/56”;
DateTime.TryParse(dateString, culture, DateTimeStyles.None, out var result);

2 Likes

Hi @Pradeep.Robot,

You can use: CDate(yourVariableName).toString("MM/dd/yyyy")

This solves the problem with the date “02/18/56”.

image

image

But your problem with the date “02/15/13”, where the year could be 1913 or 2013, I can see only one solution… which is update the records in the database to each corret year (with 4 digits) and also make sure that all new records will be inserted with the correct format (year with four digits).

If this solves your problem, kindly marks this post as solution to close this topic.

If you need extra help or/and if you have any question, please let me know :slight_smile:

Thanks!

Thanks for your time, But the solution didnt work… can you try any number from 31-49?
like “02/15/39 or 02/15/46” anything?

1 Like

Hi @Pradeep.Robot,

Both worked as expected.

What happened in your case? 2046 and 2039?

image

image

I still get the same 2049 or future date for this CDate as well.

1 Like

Hi @Pradeep.Robot,

Just to make sure, are you date correct? I am asking this, because sometimes we are using virtual machines and the date and time are totally wrong, but we don’t even notice that. Perhaps, this CDate function take into account the machine’s date and time. So, if the year is 2088 and you ask to conver 49… it will return 2049

image

@Pradeep.Robot,

Could you try the following one:

image

I tried this already and its providing the same result. For your previous system, I have EST in my system… which is 4.34 PM for me (with the same date). surprisingly, both the functionalities working for your case with the proper number and date? does it require any other settings or something like that?

1 Like

Yeah, both worked in my case.

I am not sure if it requires other settings or something else, but I am trying to find out.

Have you tried @Lucas_Ferraz 's suggestion?

image

Thanks! I am trying to incorporate @Lucas_Ferraz logic as well. TryParse will be a boolean output.
Tried to incorporate the current culture

image

it didn’t workout either. Is it working for you?

1 Like

@Pradeep.Robot,

It worked here as well :sweat_smile:

Try running this workflow

Main.xaml (5.8 KB)

Sorry, Microsoft SDK packages are not allowed with security. couldnt run this workflow as it requires the packages. cant go with invoke code option i believe.

1 Like

@Pradeep.Robot,

What about Invoke Method?

Main.xaml (6.6 KB)

Thanks @gustavo.cervelin for your time and help, This works! Appreciate your help on the same. @Lucas_Ferraz - Thanks for the solution and idea. I couldn’t invoke code due to my security issues.

2 Likes

Yeahhh!!

Finally it worked :smiley:

My pleasure @Pradeep.Robot!

Thank you @Lucas_Ferraz!

1 Like

Yes, But not sure why it varies with the previous results on yours and mine. Cdata and DateTime.Tostring and CultureInfo are most common try-outs for everyone. Not able to identify why it is producing different results.

2 Likes

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