
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 07:51 AM
Can someone show me how I can get two dates in the same format? Below is an onChange client script that runs on a date field. I need the oldValue to return in the format of M/D/Y just like the new value, see screen shot below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert("Old Value = " + oldValue + " New Value = " + newValue);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 08:47 AM
It appears that oldValue is the stored value of the field and not the display value that your selected date is. Since ServiceNow stores all of its dates in the format of YYYY-MM-DD, you should be able to take the date string from oldValue and split it into different variables to display your alert using the expected format MM-DD-YYYY
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (oldValue != '') {
dateArray = oldValue.toString().split('-');
dYear = dateArray[0];
dMonth = dateArray[1];
dDay = dateArray[2];
oldFormated = dMonth + '-' + dDay + '-' + dYear;
alert('Old Value: ' + oldFormated + ' New Value: ' + newValue);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 08:16 AM
Can you just format the dates in Javascript before returning them. It they are both Date datatypes then you should be able to format them the same. See below format function.
function formatDate(date) {
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear();
}
alert("Old Value = " + formatDate(oldValue) + " New Value = " + formatDate(newValue));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 08:47 AM
It appears that oldValue is the stored value of the field and not the display value that your selected date is. Since ServiceNow stores all of its dates in the format of YYYY-MM-DD, you should be able to take the date string from oldValue and split it into different variables to display your alert using the expected format MM-DD-YYYY
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (oldValue != '') {
dateArray = oldValue.toString().split('-');
dYear = dateArray[0];
dMonth = dateArray[1];
dDay = dateArray[2];
oldFormated = dMonth + '-' + dDay + '-' + dYear;
alert('Old Value: ' + oldFormated + ' New Value: ' + newValue);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2017 08:51 AM
Thank you Christopher - That worked perfectly