- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2024 11:13 PM
I have trouble getting GlideDateTime() and GlideDate() to perform straightforward operations. If the first numbers (DD) are under 12 it gets designed as mm-dd-yyyy. My fundamental configs and client date design are dd/MM/yyyy.
This one says 2024-09-24 isn't around the same time as 2024-09-24. I keep thinking about whether it believes its 2024-09-24 and why?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 01:33 AM
When working with date and time in ServiceNow using the GlideDateTime object, it's essential to follow the conventions set forth in the API documentation to avoid confusion and ensure accurate comparisons.
-
Setting Values:
- Use
setValue()
for UTC time in the internal formatyyyy-MM-dd HH:mm:ss
. For example:var start = '2023-07-02 00:00:00'; // Use UTC format var gdt1 = new GlideDateTime(); gdt1.setValue(start); // Correct usage for UTC
- Use
-
User Time Zones:
- To set a date/time in the current user's time zone, use
setDisplayValue()
. This interprets the input based on the user's settings:var start = '07/02/2023'; // This could be ambiguous depending on the date format var startDate = new GlideDateTime(); startDate.setDisplayValue(start); // Interpreted in the user's time zone gs.info("startDate = " + startDate);
- To set a date/time in the current user's time zone, use
-
Comparing Dates:
- Only compare dates that are set using the same methods to avoid inconsistencies:
- For UTC comparisons, use:
setValue()
withgetValue()
.
- For user time zone comparisons, use:
setDisplayValue()
withgetDisplayValue()
.
- For UTC comparisons, use:
- Only compare dates that are set using the same methods to avoid inconsistencies:
-
Internal and Local Display Values:
- If you need to work with the current user's time zone using internal formats, consider using:
setDisplayValueInternal()
andgetDisplayValueInternal()
.
- To retrieve the local date, use the
getLocalDate()
method instead of relying on display value methods, as they might lead to incorrect assumptions.
// Setting a date in UTC
var utcDate = '2023-07-02 00:00:00';
var gdtUTC = new GlideDateTime();
gdtUTC.setValue(utcDate); // Correctly set to UTC// Setting a date in the user's time zone
var userDate = '07/02/2023'; // Be cautious with date formats
var gdtUser = new GlideDateTime();
gdtUser.setDisplayValue(userDate); // Set according to user timezone// Comparing dates
if (gdtUTC.getValue() === gdtUser.getValue()) {
gs.info("The dates match in UTC.");
} else {
gs.info("The dates do not match.");
}// Getting the local date
var localDate = gdtUser.getLocalDate(); // Correctly gets the local date
gs.info("Local Date: " + localDate);
- If you need to work with the current user's time zone using internal formats, consider using:
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 01:33 AM
When working with date and time in ServiceNow using the GlideDateTime object, it's essential to follow the conventions set forth in the API documentation to avoid confusion and ensure accurate comparisons.
-
Setting Values:
- Use
setValue()
for UTC time in the internal formatyyyy-MM-dd HH:mm:ss
. For example:var start = '2023-07-02 00:00:00'; // Use UTC format var gdt1 = new GlideDateTime(); gdt1.setValue(start); // Correct usage for UTC
- Use
-
User Time Zones:
- To set a date/time in the current user's time zone, use
setDisplayValue()
. This interprets the input based on the user's settings:var start = '07/02/2023'; // This could be ambiguous depending on the date format var startDate = new GlideDateTime(); startDate.setDisplayValue(start); // Interpreted in the user's time zone gs.info("startDate = " + startDate);
- To set a date/time in the current user's time zone, use
-
Comparing Dates:
- Only compare dates that are set using the same methods to avoid inconsistencies:
- For UTC comparisons, use:
setValue()
withgetValue()
.
- For user time zone comparisons, use:
setDisplayValue()
withgetDisplayValue()
.
- For UTC comparisons, use:
- Only compare dates that are set using the same methods to avoid inconsistencies:
-
Internal and Local Display Values:
- If you need to work with the current user's time zone using internal formats, consider using:
setDisplayValueInternal()
andgetDisplayValueInternal()
.
- To retrieve the local date, use the
getLocalDate()
method instead of relying on display value methods, as they might lead to incorrect assumptions.
// Setting a date in UTC
var utcDate = '2023-07-02 00:00:00';
var gdtUTC = new GlideDateTime();
gdtUTC.setValue(utcDate); // Correctly set to UTC// Setting a date in the user's time zone
var userDate = '07/02/2023'; // Be cautious with date formats
var gdtUser = new GlideDateTime();
gdtUser.setDisplayValue(userDate); // Set according to user timezone// Comparing dates
if (gdtUTC.getValue() === gdtUser.getValue()) {
gs.info("The dates match in UTC.");
} else {
gs.info("The dates do not match.");
}// Getting the local date
var localDate = gdtUser.getLocalDate(); // Correctly gets the local date
gs.info("Local Date: " + localDate);
- If you need to work with the current user's time zone using internal formats, consider using:
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/