Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

GlideDate formatting

Wade Clairmont
Tera Guru

Quick share, relevant for other we are not sure.

 

We found this year that date formatting using

 

var gd = new GlideDate();
gd.setValue('2024-12-31');
gd.getByFormat("M/d/Y"); produces a new date adding 1 year i.e. result = 
12/31/2025
We had UI Pages formatting dates and had to update to
 
var gd = new GlideDate();
gd.setValue('2024-12-31');
var date = gd.getByFormat("M/d/y"); to produce correct date i.e. result =
12/31/2024
3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Wade Clairmont 

can you share your complete script?

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

@Wade Clairmont 

you can try this, always use GlideDateTime and then get the date

var gd = new GlideDateTime();
gd.setValue('2024-12-31 00:00:00');
var dt = new GlideDate();
dt.setValue(gd.getDate());
var date = dt.getByFormat("M/d/y"); 
gs.info(date);

Output:

AnkurBawiskar_0-1736232192469.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

yuvarajkate
Giga Guru

Try using this:

 

var gd = new GlideDate();
gd.setValue('2024-12-31');
// Correctly returns 12/31/2024
var date = gd.getByFormat("M/d/y"); 

 

 

  • "M/d/y" returns the date in MM/DD/YY format, where y correctly refers to the two-digit year (2024).
  • "M/d/Y" can sometimes cause unexpected results due to its handling of four-digit years.