The CreatorCon Call for Content is officially open! Get started here.

How to Convert Date time format of specific type to GlideDateTime

Deepak86
Kilo Contributor

I have date time value "10 Jul 2018 12:30:00". I am trying to add 7 days to it. I am using the GlideDateTime(<date_string>). I am unable to get the object so that i can add days to to it. If i use the gs.nowNoTZ() function and pass it as input to GlideDateTime it is working. I found that gs.nowNoTZ() give date in the format 2018-07-04 06:48:16. Due to this difference i am unable to get the result. My input is "10 Jul 2018 12:30:00" How can i change this format?

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

To change the format, you first need to set up GlideDateTime using your current format 

var date = '10 Jul 2018 12:30:00';
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();

gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ
gs.addInfoMessage(gdt.getDisplayValue()); // Output time in current TZ
2018-07-10 12:30:00 PM

Once your GlideDateTime object has the correct time, you can manipulate it as you need.

gdt.addDays(7);
gs.addInfoMessage(gdt.getDisplayValue());
2018-07-17 12:30:00 PM

And back again to your original format, with the added 7 days

var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();

gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));
17 Jul 2018 12:30:00


See my blog for more examples.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

To change the format, you first need to set up GlideDateTime using your current format 

var date = '10 Jul 2018 12:30:00';
var simpleDateFormat = 'dd MMMM yyyy HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime();

gdt.setDisplayValue(date,simpleDateFormat); //Set time using current TZ
gs.addInfoMessage(gdt.getDisplayValue()); // Output time in current TZ
2018-07-10 12:30:00 PM

Once your GlideDateTime object has the correct time, you can manipulate it as you need.

gdt.addDays(7);
gs.addInfoMessage(gdt.getDisplayValue());
2018-07-17 12:30:00 PM

And back again to your original format, with the added 7 days

var gd = gdt.getLocalDate();
var gt = gdt.getLocalTime();

gs.addInfoMessage(gd.getByFormat('dd MMM yyyy') + gt.getByFormat(' HH:mm:ss'));
17 Jul 2018 12:30:00


See my blog for more examples.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thanks

Please mark as correct if this has answered you question 🙂


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,

we have a similar requirement for one of a catalog item, where the date format is "dd-mmm-yy" - 10-jul-18

but it has to display as "dd-mmm-yyyy" - 10-jul-2018 and the same to be displayed in RITM AND TASK

can you provide the code for this

and it should not affect the system date format

 

thanks in advance