- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:48 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:58 AM
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