Is there a GlideDateTime variant for getByFormat?

peterraeves
Mega Guru

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;

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

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?

Ben Vargas
Tera Guru

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