Compare dates in different format

Utpal Dutta1
Mega Guru

Hi all,

We are looking for a way to compare different date formats. We store date in a string field & then we want to add a validation on server side that if the date is before 02/02/2022 then abort insertion. 

But the problem is that user who submits the record in this table, he might have different date format which will cause issues in this date comparison since we have used (dd/MM/yyyy) & user may have date format as (yyyy/MM/dd). Also before hand we don't know user will have which format in his profile.

So what I'm looking for is either a way to compare 2 different date formats or maybe convert any string date format to a common format which helps in date comparison.

 

Thanks,

Utpal

12 REPLIES 12

Hi Ankur,

 

Thank you for your reply but I want to do a server side validation. I would appreciate if you could help me with that.

 

Thanks,

Utpal

Try below code:

var gdt = new GlideDate();
var gdt1 = new GlideDate();
gdt1.setValue("02-02-2022");
gs.info(gdt);
if(gdt > gdt1)
gs.info("future");
else
gs.info("past");
Best Regards
Aman Kumar

I'm afraid it won't work when date formats are different

Any reason you wish to do server side when it can be easily done without script using UI policy?

Is this a business requirement to use server side instead of client side?

I would recommend using low-code wherever it can be done so that it can be easily maintained.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes Ankur, This is a business requirement. We have already implemented it using UI policy but seems like our Info Sec team is having concern with Client side validation & they want us to validate this in server.