convert date/time to acceptable ServiceNow field format

EdLtu5
Tera Contributor

Hi all,

 

I'm sending a "Recorded Date" field data in this format: "2023-09-28T09:01:11.71Z" to ServiceNow Date/Time Field.Currently it is being represented as "2023-09-28 00:00:00"

 

How do I need to handle it on ServiceNow side, so that the ServiceNow Date/Time field would update as this: 2023-09-28 09:01:11

 

A current script sample I use is this:

 

 

if ("Custom.RecordDate" in obj.resource.fields)
 {
gr.u_atb_stamp = obj.resource.fields["Custom.RecordDate"].newValue;
}​

 

 

Any help is much appreciated!!

 

1 ACCEPTED SOLUTION

Unfortunately that didn't helped, here how I resolved it:

 

var inputDateTime = "2023-10-02T13:54:46.383Z";

// Parse the input string as a Date object
var dateTime = new Date(inputDateTime);

// Create a formatted date and time string in UTC
function padWithZero(num) {
  return num < 10 ? "0" + num : num;
}

var formattedDateTime =
  dateTime.getUTCFullYear() + "-" +
  padWithZero(dateTime.getUTCMonth() + 1) + "-" +
  padWithZero(dateTime.getUTCDate()) + " " +
  padWithZero(dateTime.getUTCHours()) + ":" +
  padWithZero(dateTime.getUTCMinutes()) + ":" +
  padWithZero(dateTime.getUTCSeconds());

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @EdLtu5 

 

Please try the below code.

if ("Custom.RecordDate" in obj.resource.fields) {
// Get the raw date string
var rawDate = obj.resource.fields["Custom.RecordDate"].newValue;

// Create a GlideDateTime object and set it using the raw date
var gdt = new GlideDateTime();
gdt.setDisplayValue(rawDate);

// Format the date as "yyyy-MM-dd HH:mm:ss"
var formattedDate = gdt.getDisplayValue("yyyy-MM-dd HH:mm:ss");

// Update the ServiceNow field with the formatted date
gr.u_atb_stamp = formattedDate;
}

 

Unfortunately that didn't helped, here how I resolved it:

 

var inputDateTime = "2023-10-02T13:54:46.383Z";

// Parse the input string as a Date object
var dateTime = new Date(inputDateTime);

// Create a formatted date and time string in UTC
function padWithZero(num) {
  return num < 10 ? "0" + num : num;
}

var formattedDateTime =
  dateTime.getUTCFullYear() + "-" +
  padWithZero(dateTime.getUTCMonth() + 1) + "-" +
  padWithZero(dateTime.getUTCDate()) + " " +
  padWithZero(dateTime.getUTCHours()) + ":" +
  padWithZero(dateTime.getUTCMinutes()) + ":" +
  padWithZero(dateTime.getUTCSeconds());