In Client Script, how can I compare 2 dates(with time) that are in different format.

JLeong
Mega Sage

In Client Script, how can I compare 2 dates(with time) that are in different format.

ex   yyyy-mm-dd HH:mm:ss and MM-dd-yyyy hh:mm:ss (12 hours)

Somehow I need to convert the 2nd date&time to system format and then compare to the first date. But I do not know how. Please assist.

1 ACCEPTED SOLUTION

Using "getDisplayValue()" will retrieve the date/time in the person's timezone and display format.   So you could use that on both date variables to force them into the same timezone.



gdate.getDisplayValue()


View solution in original post

13 REPLIES 13

That is correct.


Did you ever find a solution to this?


No, not yet. I put it on the side for now as I am working on another thing.


Thanks for following up.


Anurag Tripathi
Mega Patron
Mega Patron

Hi Jocelyn,



Try this code:



var dateFormat=g_user_date_time_format;


var D_one = g_form.getValue("<first date>");


var D_two = g_form.getValue("<second date>");


var dateOneFormat = getDateFromFormat(D_one , g_user_date_time_format);


var dateTwoFormat = getDateFromFormat(D_two , g_user_date_time_format);


var difference=(dateOneFormat - dateTwoFormat );



Hope this solve your issue.



Here are two reference pages also which will be of help to anyone having similar issues.



https://community.servicenow.com/message/703546#703546



http://wiki.servicenow.com/index.php?title=Validate_Date_and_Time


-Anurag

Thanks a bunch Anurag. My other challenge is the timezone.


When I try to compare the dates coming from the server and the new entered date, even though they are the same, the system returns "false" bec of the timezone difference.


Do you have any fix for this?