GlideDate() / GlideDateTime() keeps swapping formats, wont give correct dates

JustinNL
Tera Contributor

Hi, Please can I get some help?

 

I'm struggling to get GlideDateTime() and GlideDate()to do simple functions. If the 1st numbers (DD) are less than 12 it gets formatted as mm-dd-yyyy. My basic configs and user date format are dd/MM/yyyy.

 

This one says 2023-02-07 is not on the same day as 2023-02-07. I wonder if it thinks its 2023-07-02 and why?

 

 

var start = '2023-02-07';
var gdt1 = new GlideDate();
var gdt2 = new GlideDate();
gdt2.setValue(start);
gs.info("Is on or after = "+gdt2.onOrAfter(gdt1));
gs.info("gdt1 = " + gdt1);
gs.info("gdt2 = " + gdt2);

*** Script: Is on or after = false
*** Script: gdt1 = 2023-02-07
*** Script: gdt2 = 2023-02-07

 

 

With GDT:

 

 

var start = '2023-02-07';
var gdt1 = new GlideDateTime();
var gdt2 = new GlideDateTime();
gdt2.setValue(start);
gs.info("Is on or after = "+gdt2.onOrAfter(gdt1));
gs.info("gdt1 = " + gdt1);
gs.info("gdt2 = " + gdt2);

*** Script: Is on or after = false
*** Script: gdt1 = 2023-02-07 16:34:04
*** Script: gdt2 = 2023-02-07 00:00:00

 

 

Using the below its now being set as MM-dd-yyyy 

 

 

var start = '07/02/2023';
var gdt1 = new GlideDateTime();
var gdt2 = new GlideDateTime();
gdt2.setValue(start);
gs.info("Is on or after = "+gdt2.onOrAfter(gdt1));
gs.info("gdt1 = " + gdt1);
gs.info("gdt2 = " + gdt2);

*** Script: Is on or after = true
*** Script: gdt1 = 2023-02-07 16:34:54
*** Script: gdt2 = 2023-07-02 00:00:00

 

 

Using 12/02/2023

 

*** Script: Is on or after = true
*** Script: gdt1 = 2023-02-07 16:36:27
*** Script: gdt2 = 2023-12-02 00:00:00

 

Using 13/02/2023 - its back to dd-MM-yyyy

 

*** Script: Is on or after = true
*** Script: gdt1 = 2023-02-07 16:37:14
*** Script: gdt2 = 2023-02-13 00:00:00

 

I tried formatting the date to find its 1 day & 1 month behind?

 

 

 var start = '07/02/2023'
var simpleDateFormat = 'dd/mm/yyyy';
        var nowDate = new GlideDateTime().getDate();
        var startDate = new GlideDateTime();
startDate.setValue(start)
startDate.setDisplayValue(start,simpleDateFormat);
var startDateFormatted = startDate.getByFormat('yyyy-MM-dd');

gs.info("Start date On or after Now = " + startDate.onOrAfter(nowDate));
gs.info ("nowDate = " + nowDate);
gs.info ("startDate = " + startDate );

*** Script: Start date On or after Now = false
*** Script: nowDate = 2023-02-07
*** Script: startDate = 2023-01-06 23:02:00

 

 

Any help appreciated.

 

Thanks

Justin

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

It's more like you are not using the methods as expected - as described by the API docs.

E.g. you write:

 

var start = '07/02/2023';
var gdt1 = new GlideDateTime();
gdt2.setValue(start);

 

However the docs say about this form of setValue:

A string in the UTC time zone and the internal format of yyyy-MM-dd HH:mm:ss.

So the system starts guessing and it guesses wrong.

The same for 12/02/2023 and 13/02/2023. The difference is that guessing in case of the letter is easier as there is no 13th month.

Then you write

var start = '07/02/2023'
var simpleDateFormat = 'dd/mm/yyyy';
var startDate = new GlideDateTime();
startDate.setDisplayValue(start, simpleDateFormat);
gs.info ("startDate = " + startDate );

in other words setting the value in the current user's TZ, but displaying it in UTC and comparing the two - unless the current user has selected UTC as his TZ, there will be no match.

 

As a general rule, when talking about date/times, you should only compare things that you set using setValue() and got using getValue().

Or things that you set using setDisplayValue() and got using getDisplayValue().

The former two will interpret and return the date/time in UTC, the latter two will interpret and return the date/time in the current user's selected TZ (and selected format).

There is also setDisplayValueInterna() and getDisplayValueInternal() which also interpret and return the date/time in the current user's TZ, but this time using the internal format.

 

With dates it is a little funkier, as getting the local date does not work using the display value methods, one needs to call the getLocalDate() method. The docs here are misleading when they state that new GlideDate().getDisplayValue() returns the date in the current user's TZ.

View solution in original post

4 REPLIES 4

Amit Gujarathi
Giga Sage
Giga Sage

HI @JustinNL ,

 

Hope you are doing great.

It looks like the issue is the display valueformat you have set for the startDate object. The display value format and the GlideDateTime constructor format are different. When you set the value with the constructor you are passing a string (start) in as the value in the format: 'YYYY-MM-DD'. When you set the display value you are passing in the string (start) in the format: 'DD/MM/YYYY'. That is why your results are off by a day and a month.

 

Regards,

Amit Gujarathi


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



-O-
Kilo Patron
Kilo Patron

It's more like you are not using the methods as expected - as described by the API docs.

E.g. you write:

 

var start = '07/02/2023';
var gdt1 = new GlideDateTime();
gdt2.setValue(start);

 

However the docs say about this form of setValue:

A string in the UTC time zone and the internal format of yyyy-MM-dd HH:mm:ss.

So the system starts guessing and it guesses wrong.

The same for 12/02/2023 and 13/02/2023. The difference is that guessing in case of the letter is easier as there is no 13th month.

Then you write

var start = '07/02/2023'
var simpleDateFormat = 'dd/mm/yyyy';
var startDate = new GlideDateTime();
startDate.setDisplayValue(start, simpleDateFormat);
gs.info ("startDate = " + startDate );

in other words setting the value in the current user's TZ, but displaying it in UTC and comparing the two - unless the current user has selected UTC as his TZ, there will be no match.

 

As a general rule, when talking about date/times, you should only compare things that you set using setValue() and got using getValue().

Or things that you set using setDisplayValue() and got using getDisplayValue().

The former two will interpret and return the date/time in UTC, the latter two will interpret and return the date/time in the current user's selected TZ (and selected format).

There is also setDisplayValueInterna() and getDisplayValueInternal() which also interpret and return the date/time in the current user's TZ, but this time using the internal format.

 

With dates it is a little funkier, as getting the local date does not work using the display value methods, one needs to call the getLocalDate() method. The docs here are misleading when they state that new GlideDate().getDisplayValue() returns the date in the current user's TZ.

KetilE
Tera Contributor

Thanks for the write up, it helped me get over the hurdle I was struggling with. However, I would like to point out that the simpleDateFormat is incorrect. The format is case sensitive, where lower case 'mm' evaluates to minutes and higher case 'MM' evaluates to months. So in this case you would have to use: 

 

var simpleDateFormat = 'dd/MM/yyyy';

 

Here is a list of some of the common symbols for Date formatting:

dd - Day of the month
MM - Month in year
yyyy - Year
HH - Hour in 24-hour format
hh - Hour in 12-hour format
mm - Minutes
ss - Seconds

Yes, you're right.