- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 05:49 AM
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!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 08:15 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 06:13 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2023 08:15 AM
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());