Need help with date/time field converting to string

Nowlearner
Kilo Guru

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
}

]}

}

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

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.

jio_escoto
Tera Contributor

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!

Shweta KHAJAPUR
Tera Guru

Hi Nikitha,

Modify your code as below,

if (variables.region == 'UK') {
date = [year,month,day].join('-');
} else {
date = [year,month,day].join('-');
}
}

Kalaiarasan Pus
Giga Sage

This shows one of the ways to format the date time field

https://community.servicenow.com/community?id=community_question&sys_id=00dcc329db9cdbc01dcaf3231f961905