Is there a GlideDateTime variant for getByFormat?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2018 03:41 AM
I have a GlideDateTime and want to print it with the given format. I noticed that there is the getByFormat function for GlideDate and GlideTime, but not for GlideDateTime... Did I just not find it, wast here a specific reason for not implementing it or do I have an alternative for this?
Edit:
This is what I use now and it was created in just a few minutes. So I'm curious, why ServiceNow doesn't provide this ootb. What am I missing?
var format = 'dd/MM/yyyy hh:mm:ss'.split(' ');
var gdt = new GlideDateTime();
var formatDate = format[0];
var formatTime = format[1];
var gd = gdt.getLocalDate().getByFormat(formatDate);
var gt = gdt.getLocalTime().getByFormat(formatTime);
var output = gd + ' ' + gt;
- Labels:
-
Scripting and Coding
- 9,413 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2018 06:57 AM
Hi Peter,
Is this functionality working fine?
If yes then what is your question. Since the method is provided by ServiceNow.
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2018 07:13 AM
This functionality works, yes.
My question is, whether there is an ootb way to do this, instead of having to script it myself? If there is a 'getByFormat' function for the GlideDateTime object? The fact that I cannot find it is confusing, since it seems to be easy to create. So I wonder why they did not do so. There might be a reason behind this? Some issue I am possibly overlooking?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2021 03:51 PM
I ran into this question as well... you can use the GlideDate Object with time values and then the getByFormat() method to get the format you need.
Here is a script that can be run in Scripts - Background to validate this:
var incidentGR = new GlideRecord('incident');
incidentGR.get('some_valid_incident_sys_id_here');
gs.log('updated: ' + incidentGR.sys_updated_on);
var gd = new GlideDate();
gd.setValue(incidentGR.getValue('sys_updated_on'));
gs.log('updated ISO: ' + gd.getByFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
Example output:
[0:00:00.196] Script completed in scope global: script
Script execution history and recovery available here
*** Script: updated: 2021-07-26 22:23:58
*** Script: updated ISO: 2021-07-26T22:23:58.000+0000