How to compare different date format without convert to string

I got an If Condition (CurrentYearAnniversary < datetime.Today).
But because both have different date format, it failed to compare properly.
CurrentYearAnniversary is declared as DateTime variable with value assign to it.

Below is the output of both:
CurrentYearAnniversary: 09/12/2022 00:00:00
datetime.Today: 01/28/2022 00:00:00

Below is the format of the output:
CurrentYearAnniversary: dd/MM/yyyy 00:00:00
datetime.Today: MM/dd/yyyy 00:00:00

How do I change datetime.Today format to “dd/MM/yyyy 00:00:00” ?

Hi @Wong_Chee_Mun

Try the below expression

Cdate(“01/28/2022 00:00:00”).tostring(“dd/MM/yyyy”)

Regards
Gokul

But I cant compare in the IF statement if convert to string.
In the IF statement, both are DateTime variable.

1 Like

Hey @Wong_Chee_Mun

  • Date format doesn’t matter during the date type object comparison

  • If you have different timezones then it may affect the same

  • For that you may use .ToUniversalTime method to convert and compare

No where in the above approaches we use string conversion

Hope this helps

Thanks
#nK

1 Like

Hi @Nithinkrishna,

Thanks for the info.
I just found out the CurrentYearAnniversary is in American date format(MM/dd/yyyy).
So my tutorial is working correctly.

I’m from Malaysia and and we always use (dd/MM/yyyy) format.
So if I’m dealing with date, I just need to assign the date, month, year properly first.
Then perform activities using American date format mindset.

1 Like

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