Converting to date format

Misgun
Tera Contributor

While creating a transform map I wanted to create a transform script which accepts date as a string (20181025) and convert that to a date format in ServiceNow.

1 ACCEPTED SOLUTION

Misgana
Tera Guru

@Misgun 

In order to convert the string to date format the string must have '-' between them like YY-MM-DD, to make that happen you must slice the string into three places then push it to an array, and then convert that to date format using the DateTime object.

 

var text = 20181025;

var year = text.slice(0,4);

var month = text.slice(4,6);

var day = text.slice(6,8);

var Array = [];

Array.push(year);

Array.push(month);

Array.push(day);

var formattedDate = Array.join('-'); //output 2018-10-25 as a string

var dateFormat = new GlideDateTime(formattedDate);

dateFormat = dateFormat.getDate(); //output will be 2018-10-25 in a date format

 

View solution in original post

2 REPLIES 2

Misgana
Tera Guru

@Misgun 

In order to convert the string to date format the string must have '-' between them like YY-MM-DD, to make that happen you must slice the string into three places then push it to an array, and then convert that to date format using the DateTime object.

 

var text = 20181025;

var year = text.slice(0,4);

var month = text.slice(4,6);

var day = text.slice(6,8);

var Array = [];

Array.push(year);

Array.push(month);

Array.push(day);

var formattedDate = Array.join('-'); //output 2018-10-25 as a string

var dateFormat = new GlideDateTime(formattedDate);

dateFormat = dateFormat.getDate(); //output will be 2018-10-25 in a date format

 

Akshay37
Mega Guru

Hi ,

 

You can use  new GlideDate(datestring);  in script to convert it to servicenow date.

 

Please mark it helpful/correct if this helps you.

 

Thanks,

Akshay