GlideDate() and GlideDateTime() keep switching formats, resulting in inaccurate dates. Could you ple

SandeepKSingh
Kilo Sage

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?

1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

 

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.

 

  1. Setting Values:

    • Use setValue() for UTC time in the internal format yyyy-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
  2. 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);
  3. Comparing Dates:

    • Only compare dates that are set using the same methods to avoid inconsistencies:
      • For UTC comparisons, use:
        • setValue() with getValue().
      • For user time zone comparisons, use:
        • setDisplayValue() with getDisplayValue().
  4. Internal and Local Display Values:

    • If you need to work with the current user's time zone using internal formats, consider using:
      • setDisplayValueInternal() and getDisplayValueInternal().
    • 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 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/

View solution in original post

1 REPLY 1

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

 

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.

 

  1. Setting Values:

    • Use setValue() for UTC time in the internal format yyyy-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
  2. 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);
  3. Comparing Dates:

    • Only compare dates that are set using the same methods to avoid inconsistencies:
      • For UTC comparisons, use:
        • setValue() with getValue().
      • For user time zone comparisons, use:
        • setDisplayValue() with getDisplayValue().
  4. Internal and Local Display Values:

    • If you need to work with the current user's time zone using internal formats, consider using:
      • setDisplayValueInternal() and getDisplayValueInternal().
    • 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 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/