The CreatorCon Call for Content is officially open! Get started here.

ServiceNow Learning 43: Useful Data Conversion from "Sep 21 2023 1:40PM" to "21/09/2023"

Shamma Negi
Kilo Sage
Kilo Sage

Hi Team,

 

Here is very useful good example on converting custom date from this format "Sep 21 2023 1:40PM" to "21/09/2023". ServiceNow doesnt convert it by its own and shows this message when you dont convert by writing below script. Error Message : Cannot convert NaN to java.ling

 

Import set: ISET4022133 transform stopped due to error: java.lang.NullPointerException

 

Unable to format Oct  2 2023  6:28PM using format string yyyy-MM-dd HH:mm:ss for field process_date

 

Server Script: I wrote in transform map

 

var dd23 = source.u_date; //u_date is in format "Sep 21 2023 1:40PM"

var dd12 = dd23.substring(0, 11);

var tmpDate = new Date(dd12);

var shortDate = new convertDate(tmpDate);

var finaldate = shortDate.replaceAll("/" , "-");

return finaldate;//dd/mm/yyyy// return the value to be put into the target field

 

 

Script Include:

 

var tmpDate = new Date(dd2);
var shortDate = convertDate(tmpDate);
return shortDate;

 

function convertDate(inputFormat) {
function pad(s) {
return (s < 10) ? '0' + s : s;
}
var d = new Date(inputFormat);
return [pad(d.getMonth() + 1), pad(d.getDate()), d.getFullYear()].join('-');
}

 

Hope this helps.

Please mark helpful if it helps you.

Regards,

Shamma

Regards,Shamma Negi
0 REPLIES 0