GlideDateTime not working as expected.

Kingstan M
Kilo Sage

Hi SNC,

Please see this, I do not understand why this return false?

I get false for >> gs.info(today>incoming);

var today = gs.nowDateTime()
var incoming = '28-08-2022 12:00 AM';

gs.info(today);
gs.info(incoming);
gs.info(today>incoming);
1 ACCEPTED SOLUTION

Kingstan M
Kilo Sage

This below code works >>

use getValue() method instead of getDisplayValue()

Reference : Community_Page

var today = gs.nowDateTime();
var incoming =  '28-08-2022 12:00 AM';


var gdt_incoming = new GlideDateTime();
gdt_incoming.setDisplayValue(incoming, "dd-MM-yyy hh:mm a");
var gdt_incoming_converted = gdt_incoming.getValue();
gs.info('gdt_incoming_converted : ' + gdt_incoming_converted);

var gdt_today = new GlideDateTime();
gdt_today.setDisplayValue(today, "dd-MM-yyy hh:mm a");
var gdt_today_converted = gdt_today.getValue();
gs.info('gdt_today_converted : ' + gdt_today_converted)

gs.info(gdt_today_converted > gdt_incoming_converted);

View solution in original post

9 REPLIES 9

Kalyani Jangam1
Mega Sage
Mega Sage

var today = new GlideDateTime();
var incoming = '28-08-2022 12:00 AM';

gs.info(today);
gs.info(incoming);
gs.info(today>incoming);

Nick Parsons
Mega Sage

Why is it returning false?

You're comparing two strings, not two date objects. When strings are compared using >, they're compared character by character from left to right, since today holds "02/09/2022 14:51:57" it will start by comparing the first characters of your two strings, so "0" > "2" (ie: today[0] > incoming[0]) in your case. "0" is smaller than "2" so the today>incoming between your two strings evaluates to false

Suggested fix

I would suggest making both of your dates GlideDateTime objects and then using the .after() method, eg:

var today = new GlideDateTime(); // now
var incoming = new GlideDateTime(); 
incoming.setDisplayValue("2022-08-28 00:00:00"); // local time zone specified with the format yyyy-MM-dd HH:mm:ss.

gs.info(today.getDisplayValue()); // logs local time, use `gs.info(today)` for the UTC time
gs.info(incoming.getDisplayValue()); // logs local time (ie: "2022-08-28 00:00:00"), use gs.info(incoming) for UTC
gs.info(today.after(incoming)); // basically does `today > incoming` between your two dates

 

*** Script: patchStartGDT : 25-09-2022 08:13 PM
*** Script: todaygdt : 02-09-2022 06:09 PM
*** Script: false

var dateStart = "25-09-2022 08:13 PM";

var gdtStart = new GlideDateTime();
gdtStart.setDisplayValue(dateStart, "dd-MM-yyy hh:mm a");
var patchStartGDT = gdtStart.getDisplayValue();
gs.info('patchStartGDT : ' + patchStartGDT);

var today = new GlideDateTime();
var todaygdt = today.getDisplayValue();
gs.info('todaygdt : ' + todaygdt);
gs.info(todaygdt > patchStartGDT);

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you are not giving the correct date/time

whenever you want to compare date/time ensure you use GlideDateTime() class

your script will look like this

1) don't use gs.nowDateTime() but use new GlideDateTime() to get today/current date/time

var today = new GlideDateTime();
var incoming = new GlideDateTime('2022-08-22 00:00:00');

gs.info(today);
gs.info(incoming);
gs.info(today.getNumericValue() > incoming.getNumericValue());

Regards
Ankur

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