GlideDate formatting
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 07:28 AM
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 07:03 PM
can you share your complete script?
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 10:43 PM
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 10:21 PM
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.