Need to convert Date Time Format from JSON to Service now format

Chandu12
Mega Expert

Experts,

Need to convert the date from JSON format to service now format

JSON Format [2018-12-11T16:57:19.391396Z] --> SN Format [2018-12-11 16:57:19]

need a script for this can anyone help me ???

 

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi Chandu,

You can write like this.

First, convert the JSON object to string and then use the code below.

//JSON Format [2018-12-11T16:57:19.391396Z] --> SN Format [2018-12-11 16:57:19]
var date_json = "2018-12-11T16:57:19.391396Z"; //assuming date_json will have the string json object.
var dates = date_json.split("T");
var dates1 = dates[1].split(".");
var snow_date = dates[0]+" "+dates[1];
var gdt = new GlideDateTime(snow_date);
gs.print("Date is"+gdt.getDisplayValue());

Mark the comment as a correct answer and also helpful once worked.

View solution in original post

3 REPLIES 3

asifnoor
Kilo Patron

Hi Chandu,

You can write like this.

First, convert the JSON object to string and then use the code below.

//JSON Format [2018-12-11T16:57:19.391396Z] --> SN Format [2018-12-11 16:57:19]
var date_json = "2018-12-11T16:57:19.391396Z"; //assuming date_json will have the string json object.
var dates = date_json.split("T");
var dates1 = dates[1].split(".");
var snow_date = dates[0]+" "+dates[1];
var gdt = new GlideDateTime(snow_date);
gs.print("Date is"+gdt.getDisplayValue());

Mark the comment as a correct answer and also helpful once worked.

ggg
Giga Guru

get date value as string:

var d = '2018-12-11T16:57:19.391396Z';

var d2 = new GlideDateTime('2018-12-11T16:57:19.391396Z');

var s = d.substring(0, 10);

var p1 = d.substring(0, 10);

var p2 = d.substring(11, 19);

gs.print(p1);

gs.print(p2);

var d3 = new GlideDateTime();
d3.setValue(p1 + ' ' + p2);
gs.print(d3);

warren felsh
Kilo Explorer


The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it. JSON does not know anything about dates. What .NET does is a non-standard hack/extension. The problem with JSON date and really JavaScript in general – is that there’s no equivalent literal representation for dates. In JavaScript following Date constructor straight away converts the milliseconds since 1970 to Date as follows:

var jsonDate = new Date(1297246301973);

Then let’s convert it to js format:

var date = new Date(parseInt(jsonDate.substr(6)));

The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .

For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

var date = new Date(jsonDate);