error in comparing two dates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2024 04:28 AM
hello all my code doesn't seem to work any help?
var currentTime = new GlideDateTime();
var closedtime = new GlideDateTime(current.close_at);
var result = gs.dateDiff(closedtime, currentTime, true);
if ((current.state == '3' || current.state == '4' || current.state == '7') && result > 120) {
answer = false;
} else {
answer = true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2024 05:19 AM
please use GlideDateTime subtract method and ensure field names are correct
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2024 05:19 AM
Hi @yoli1 ,
To calculate the difference you must convert both glidedatetime dates to milliseconds using getnumercivalue and once you have the difference you can implement your logic:
You can do like below:
var currentTime = new GlideDateTime();
var date1 = currenttime.getNumericValue();
Similarly you can convert the closed_at time , you can get the difference in milliseconds and then you can convert the milliseconds into whatever format you need to compare the result in hours / minutes/seconds.
If you want milliseconds in date format you can use setnumericvalue function available.
Kindly mark helpful/correct if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2024 05:20 AM
Hi @yoli1, OOB field name of Closed is 'closed_at'. Can you recheck the line 2 in your script?
var closedDatetime = new GlideDateTime(current.close_at);
You made need to change it to
var closedDatetime = new GlideDateTime(current.closed_at);
Regards,
Sunil