Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getting invalid date and time format

String
Kilo Sage

Hi team ,

am using the below code to get the date and time 

 

var gdt = new GlideDateTime();
var date = gdt.getDate();
var time = gdt.getTime();
gs.info(date+"T"+time);

 

output:

2023-02-27T1970-01-01 13:06:26

 

but am getting  1970-01-01  ?

1 ACCEPTED SOLUTION

Faizeal Mohamed
Tera Guru

Hi,

If you need date and time you can use this

 

var gdt = new GlideDateTime();

gs.info(gdt.getDisplayValue()); //Return the date time in the logged in user time zone

 

If you need date time in specific format, please use the below script:

 

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

gs.print(gd.getByFormat('dd-MM-yy') + gt.getByFormat(' HH:mm:ss'));
 
Please try it and let me know.
 
Thanks,
Faizeal.

 

View solution in original post

5 REPLIES 5

Riyaz Ali Moham
Mega Guru

Hi @String ,
Use below code to get time the way you wanted

var gdt = new GlideDateTime().toString();
var dateTime=[];
dateTime=gdt.split(" ");
var time=dateTime[1];
gs.info(gdt);
gs.info(time);
Output:
*** Script: 2023-02-27 13:57:19
*** Script: 13:57:19