Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Glide Date Time Conversion

sam352120
Kilo Guru

HI All,

Facing an error message like : (Unable to format undefined-undefined-Nov 22, using format string yyyy-MM-dd HH:mm:ss ) while loading data through transform map to the Service Now target table.

Source date time field format: Jan 17, 2019 03:18 PM   /   Target date field format: 25-03-2019 22:50:03 (Glide date time format).

I tried to achieve that through scripting in the field mapping of the transform map,But could not succeed.Let me know your inputs.

Script Use:

answer = (function transformEntry(source) {

// Add your code here
var str=source.u_shipping_date;
var strt1 = strt.split(" ");
//gs.print(strt1[0]);
var g = strt1[0].split("-");
//gs.print(g[0]);
//gs.print(g[1]+"-"+g[2]+"-"+g[0]+" "+strt1[1]);
return g[1]+"-"+g[2]+"-"+g[0]+" "+strt1[1]; // return the value to be put into the target field

})(source);

Thanks,

Sambit

 

 

1 ACCEPTED SOLUTION

This should work

answer = (function transformEntry(source) {

var date = source.u_shipping_date; // example 'Jan 17, 2019 03:18 PM';
var simpleDateFormat = 'MMM dd, yyyy hh:mm a';
var gdt = new GlideDateTime();
gdt.setDisplayValue(date,simpleDateFormat);

return gdt.getDisplayValue();
}(source);

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

12 REPLIES 12

Simon Christens
Kilo Sage

Hi

Whats the timezone for your input:
Jan 17, 2019 03:18 PM 

You need to be sure that the timeZone is correct because GlideDateTime uses UTC value

var dateString = 'Jan 17, 2019 03:18 PM';
var d = new Date(dateString + ' GMT'); //GMT is UTC time - use ' GMT-3' if the timeZone is -3 etc
var ms = d.getTime();

var gdt = new GlideDateTime();
gdt.setNumericValue(ms);

gs.addInfoMessage(gdt); //This should be 2019-01-17 15:18:00 as UTC time

Check this out

Date is a Javascript library that might be better at handling these formats:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

With the above code i get: 

 

gdt variable: 2019-01-17 15:18:00

Script changed to handle timeZones as well

Sanaa
Kilo Contributor

hello sam352120,

before splitting your source date (Jan 17, 2019 03:18 PM), you must formatting this Source datetime.

this link that might help:

https://community.servicenow.com/community?id=community_blog&sys_id=bc0e6a2ddbd0dbc01dcaf3231f961931

Sanaa

 

Thanks for sharing my blog!


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Sanaa
Kilo Contributor

Hello sam352120,

please test this code:

var date = 'Jan 17, 2019 03:18 PM';

var gdt = new GlideDateTime();
gdt.setDisplayValue(date,"MMMM dd, yyyy K:mm a");
var dateTimeField = gdt.getDisplayValue();
gs.print(dateTimeField);

var newDate=dateTimeField.replace("/","-");
gs.print(newDate);

i got this result: 17-01-2019 15:18:00