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

Uncle Rob
Kilo Patron

It looks like you need to specify a format:
Try 

gs.info(time.getByFormat('hh:mm:ss'));

Naga Ravindra R
Kilo Sage

Hi @String 

 

Try below:

var gdt = new GlideDateTime();
var time = gdt.getTime();
gs.info(time.getByFormat('hh:mm:ss'));
 
Please get back if there are any other questions.
Please mark as helpful/correct if it helps.

Prince Arora
Tera Sage

@String ,

 

Please try below method:


var gdt= new GlideDateTime();
gs.info(gdt.getDisplayValueInternal());

 

PRINCE_ARORA_0-1677504760525.png

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

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.