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.

Convert String to GlideDateTime

Brendan Hallida
Kilo Guru

Hi there,

Trying to convert a string to a glidedatetime object.

var date = '20/07/2020 8:00:10 AM';
var gdt = new GlideDateTime();
gdt.setDisplayValue(date, "DD/MM/YYYY K:mm:ss a");
var dateTimeForField = gdt.getDisplayValue();
gs.info('d: ' + dateTimeForField);

The output I get from the gs.info has a completely incorrect date, however, the time is working correctly.

d2: 2019-12-29 08:00:10

Does anyone know what I am missing here?

Cheers, Brendan

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

try now

 

var date = '20/07/2020 8:00:10 AM';
var gdt = new GlideDateTime();
gdt.setDisplayValue(date, "dd/MM/yyyy K:mm:ss a");
var dateTimeForField = gdt.getDisplayValue();
gs.info('d: ' + dateTimeForField);

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

try now

 

var date = '20/07/2020 8:00:10 AM';
var gdt = new GlideDateTime();
gdt.setDisplayValue(date, "dd/MM/yyyy K:mm:ss a");
var dateTimeForField = gdt.getDisplayValue();
gs.info('d: ' + dateTimeForField);

Akash Gurram1
Giga Guru

As Harshavardhan has suggested, the above format needs to be used according to ServiceNow documentation.

find_real_file.png

Brendan Hallida
Kilo Guru

thanks, that is what it was.  I was using DD/MM/YYYY instead, you need to use dd/MM/yyyy as shown by Akash!