Need help with date/time field converting to string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 05:50 PM
Hi,
I have the below script include, my start date field is date/time field.
With the below script, start date field output is being displayed as: MM - DD TIME - Year
For example: 08-05 00:00:00- 2018
i need the output as: 08-05-2018 00:00:00
var sc_include = function () {
var pencil = new cr_pencil();
return {
biglist: biglist,
type: 'sc_include'
};
function biglist(variables) {
var date = variables.start_date;
if (date) {
date = date.toString();
var day = date.split('-')[2];
var month = date.split('-')[1];
var year = date.split('-')[0];
if (variables.region == 'UK') {
date = [day, month, year].join('-');
} else {
date = [month, day, year].join('-');
}
}
return [{
condition: true,
label: '\n' + 'Start date: ',
value: date
}
]}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 07:41 PM
You can try
var sc_include = function () {
var pencil = new cr_pencil();
return {
biglist: biglist,
type: 'sc_include'
};
function biglist(variables) {
var date = variables.start_date;
var dateA = date.split(' ');
if (dateA) {
date = dateA[1].toString();
var day = date.split('-')[2];
var month = date.split('-')[1];
var year = date.split('-')[0];
if (variables.region == 'UK') {
date = [day, month, year].join('-')+' '+dateA[1].toString();
} else {
date = [month, day, year].join('-')+' '+dateA[1].toString();
}
}
return [{
condition: true,
label: '\n' + 'Start date: ',
value: date
}
]}
}
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2018 11:46 PM
Why don't you use GlideDateTime instead? You can get your date value matching user's date/time format:
var date = new GlideDateTime(variables.state_date);
date.getValue(); // this should output the date in the user's date/time format
date.getDisplayValue(); // adjusts the date/time to the user's timezone
Let me know if this helps. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-06-2018 12:04 AM
Hi Nikitha,
Modify your code as below,
if (variables.region == 'UK') {
date = [year,month,day].join('-');
} else {
date = [year,month,day].join('-');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-06-2018 12:11 AM
This shows one of the ways to format the date time field
https://community.servicenow.com/community?id=community_question&sys_id=00dcc329db9cdbc01dcaf3231f961905