How to compare datetime variable (with MM/dd/yyyy hh:mm:ss format)?

Hi Guys,

I have two variables (defined as datetime)

latest_month and invoice_month

I intend to compare these two in a if loop, one data set of them is:

latest_month: 08/01/2019 00:00:00
invoice_month: 10/01/2018 00:00:00

But the result seems not really got compared in a numerical sense.

My if condition looks like this:
latest_month < invoice_month

Anybody could kindly help me out to enlighten me abit:

  1. Should I further convert them into a MMM yyyy format for an easy comparison
  2. Why my existing logic not working?

Thanks!

@Victor_Victory,

use DateTime.Compare(latest_month, invoice_month), do not use < or > operators to compare datetime. If those are with datetime datatype then it will work otherwise it will be compared as string.

hello @Victor_Victory

both latest_month and invoice_month is of type system.dateTime ?

if not please convert both variable to datetime like below.

Latest_Month_Datetime = cdate(latest_month)
invoice_month_Datetime = Cdate(invoice_month)

then you can continue with your logic.

regards
Ajay

1 Like

Hi Ajay,

If i have placed these two assign actions prior comparing them, can I consider them as system.dateTime?

dateTime_latestMonth = Convert.ToDateTime(str_latestMonth)
dateTime_invoiceMonth = Convert.ToDateTime(str_invoiceMonth)

Thanks!

hi @Victor_Victory yes it is perfectly alright.

are you still facing issue??

Thanks Ajay. Issue resolved.

1 Like

yeah @Victor_Victory welcome :slight_smile:

1 Like

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