- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 09:42 PM
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);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2022 01:51 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:02 PM
var today = new GlideDateTime();
var incoming = '28-08-2022 12:00 AM';
gs.info(today);
gs.info(incoming);
gs.info(today>incoming);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:06 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2022 01:00 AM
*** 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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 10:58 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader