Date time Comparison

Hi,

I am doing date time comparison. I got the below date and time: getting both date and time equal but when comapring it shows false

str_attached_files_date
image

maxDate
image

image
image

you may try the below expression:

str_attached_files_date.tostring.trim.equals(maxDate.tostring.trim)

Hi @marina.dutta Your second date value is having extra space between time and date
Check once
There is additional space between 2023 and 2:15

Regards,
Jagravi

@marina.dutta,

Comparing date strings is not a good practice.

Convert the your datetime strings into date and then do the comparison.

Convert Files date to DateTime datatype variable:

dtmFilesDate = DateTime.ParseExact(str_attached_files_date. Trim.Replace(" "c, ""), "M/d/yyyyh:mm", System.Globalization.CultureInfo.InvariantCulture)

Convert Max Date to DateTime datatype:

dtmMaxDate = DateTime.ParseExact(maxDate. Trim.Replace(" "c, ""), "M/d/yyyyh:mm", System.Globalization.CultureInfo.InvariantCulture)

Now compare the date to date like this:

dtmFilesDate=dtmMaxDate

Thanks,
Ashok :slightly_smiling_face:

@ashokkarale

Actually my date is in last modified column. I need to find the latest date and time in Last Modified column

image

@ashokkarale

How can I compare the max date and time in the below format?

image

@marina.dutta,

Ok in that case, use array. Add these datetime strings in an array and then sort the array.

Follow this thread for solution.

@ashokkarale

Actually its a table where I need to download the the file with max date and to get the max date by
I converted into data table and tried getting max date by

max date= ExtractDataTable_Invoice.AsEnumerable().Max(Function (drRows) DateTime.Parse(drRows.Item(“Column-1”).ToString)).ToString

Then doing for each loop and checking if current date is equal to max date. But here I am getting in PM value .I dont need the PM

@marina.dutta

Then get the datetime and just convert as you need

DateTime.ParseExact("ValueFromPage".Replace(" ",""),"M/d/yyyyh:mtt",System.Globalization.CultureInfo.InvariantCulture)

Replacing space to ensure removal of extra spaces

Now if you need in any specific format use .ToString("Format you need")

Cheers

@Anil_G

I need the dates in format like 1/19/2023 2.14

can i do it as below:

DateTime.ParseExact(“str_attached_files_date”.Replace(" “,”"),“M/d/yyyyh:mtt”,System.Globalization.CultureInfo.InvariantCulture).ToString(“M/d/yyyyh:mm”)

@marina.dutta

Just add a space after yyyy and it should be good

Cheers

@ashokkarale

the expression is not working for me.

I did in the below form

max_date=DateTime.ParseExact(max_date, “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).ToString(“M/d/yyyy h:mm”)

str_attached_files_date= DateTime.ParseExact(str_attached_files_date, “M/d/yyyy h:mm tt”, System.Globalization.CultureInfo.InvariantCulture).ToString(“M/d/yyyy h:mm”)

Then I did trim ,its working

max_date.ToString.trim>(str_attached_files_date.ToString.trim)