How to check given date is in MM/DD/YYYY format


#1

Hi,

I am getting date from excel and just I want to check that given date is in MM/DD/YYYY format or not?

If date is 30/31/2018 then return false.
If date is 04/11/2018 then return true.

Please help me.

Regards,

Neeraj Yadav


#2

Is it stored in excel as a date, or as text?

Depending on format, you can convert it to a datetime variable, then specify whatever format you’d like. Your example would be datetime.tostring(“MM/dd/yyyy”)


#3

as a date


#4

UiPath is pretty good about recognizing dates in excel as actual dates. Just save the range as a datatable (the date values will be stored as datetime in the table), or save the cell directly as a datetime variable. Then you can use it throughout the rest of your workflow with the format you need


#5

Hi Dave,

Just I want to check the date value as in below given example.

If date is 30/31/2018 then return false.
If date is 04/11/2018 then return true.


#6

Hi @neerajmca5,

string inputString = “2000-02-02”;
DateTime dDate;

if (DateTime.TryParse(inputString, out dDate))
{
    String.Format("{0:d/MM/yyyy}", dDate); 
}
else
{
    Console.WriteLine("Invalid"); // <-- Control flow goes here

Refer this

Regards,
Arivu


#7

But this concept is not working in uipath.


#8

Again, since you’re getting the date from excel, just use date.tostring(MM/dd/yyyy) in your workflow. That will always be in the format you’re looking for.

For a different use case where you are getting the date in string format already, you should use the datetime.tryparse as @arivu96 has written out and linked. Note that he is writing actual code rather than using the UiPath workflow. If you want to copy+paste you can do it in an invoke code activity. Otherwise, you can recreate it using standard activities such as If activity, along with an assign activity.


#9

Sharing this from my past learning,

Why it didn’t work in Assign/IF activity?

How can we resolve ? Existing sample for Double.TryParse

Solution: Use TryParseExact/TryParse in Invoke Code/Method

Date_TryParseExact_InvokeMethod.xaml (13.5 KB)

Note: You can also use TryParse for your requirement

MSDN References:

  1. DateTime.TryParse Method (String, DateTime)
  2. DateTime.TryParseExact Method (String, String[], IFormatProvider, DateTimeStyles, DateTime)

Get a date in formart "dd/mm/yyyy"
#10

Hi @neerajmca5,

dDate->DateTime variable
False ->Convert.ToString(DateTime.TryParse("30/31/2018",dDate))
True -> Convert.ToString(DateTime.TryParse("04/11/2018",dDate))

In If condition you can use like as
False ->DateTime.TryParse("30/31/2018",dDate)
True -> DateTime.TryParse("04/11/2018",dDate)

Regards,
Arivu


#11

Hey Arivu,

How do i use the above logic for this usecaes:

09/06/2018
06/09/2018

Thanks
Joshi


#12

Hi ,

Both the date format are same??


#13

@Joshikumarav
below will give you a boolean value

Date.TryParseExact(strDate,“dd/MM/yyyy”,Nothing,Globalization.DateTimeStyles.None,Nothing).ToString


#14

@akila93 No there is no format on the column. Apparently the excel comes from various sources and each of them contains different formats of date.

@AkshaySandhu - your code may return true for both my cases. But, I need to extract the date from 06/09/2018 and 09/06/2018. Is there any way?


#15

@Joshikumarav
if you know the date format for given string then yes…
Date.ParseExact(Str,DateFormatOfString,Nothing)