DateTime not able to set as 00:00:00 - why does it show time drift

ashwinipingle
Tera Guru

Hello All,

I am trying to set a DateTime field on pm_project record using a script. I want to set the date only and want the time as 00:00:00. However when I set the value thru a background script, it always puts a value of time as 08:00:00

Following is the script

 

// Define the list of project sys_ids and corresponding dates
var projectSysIDs = [
    'bb27e5b8c3f34250383514a4e40131e5'
]; // replace with your actual sys_ids

var projectDates = [
    '2024-11-12'
]; // replace with your actual dates
//yyyy-mm-dd
if (projectSysIDs.length === 0) {
    gs.print('No sys_id values provided.');
} else if (projectSysIDs.length !== projectDates.length) {
    gs.print('The number of sys_ids does not match the number of dates.');
} else {
    // Create a GlideRecord instance for the 'pm_project' table
    for (var i = 0; i < projectSysIDs.length; i++) {
        var sysId = projectSysIDs[i];
        var dateStr = projectDates[i] + ' 00:00:00'; // concatenate date with time set to 00:00:00
        //gs.print('dateStr: ' + dateStr);
        gs.print('Original date string: ' + dateStr);

        // Create a GlideDateTime object
        var dateTime = new GlideDateTime();
        dateTime.setDisplayValue(dateStr);

       // Convert the GlideDateTime object to UTC
        dateTime.convertToTZ('UTC');
        gs.print('dateTime in UTC: ' + dateTime.getDisplayValue());

        // Create a GlideDateTime object
       // var dateTime = new GlideDateTime();
        //dateTime.setDisplayValue(dateStr); // setting the date and time in the instance's time zone
        //gs.print('dateTime: ' + dateTime);

        var gr = new GlideRecord('pm_project');
        if (gr.get(sysId)) {
            // Print original date value for debugging purposes
            gs.print('Updating Project: ' + gr.getValue('short_description'));
            gs.print('Old Date Value: ' + gr.getValue('u_project_won_date'));

            // Set the custom date field with the new date-time value
            gr.setValue('u_project_won_date', dateTime);

            // Print the new value for debugging purposes
            gs.print('New Date Value: ' + gr.getValue('u_project_won_date'));

            // Update the record in the database
            gr.update();
        } else {
            gs.print('Project with sys_id ' + sysId + ' not found.');
        }
    }
}
 
And following is the output
*** Script: Original date string: 2024-11-12 00:00:00
*** Script: dateTime in UTC: 2024-11-12 00:00:00
*** Script: Updating Project: CNH - Strategic sourcing program-92284119
*** Script: Old Date Value: 2024-11-12 08:00:00
*** Script: New Date Value: 2024-11-12 08:00:00
 
Why does it put a "08" in the hours ? How can we fix this to set a value of 00:00:00?
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ashwinipingle 

try this

when you use setValue() on GlideDateTime, it sets value in UTC and no need to convert to timezone

// Define the list of project sys_ids and corresponding dates
var projectSysIDs = [
    'bb27e5b8c3f34250383514a4e40131e5'
]; // replace with your actual sys_ids

var projectDates = [
    '2024-11-12'
]; // replace with your actual dates
//yyyy-mm-dd
if (projectSysIDs.length === 0) {
    gs.print('No sys_id values provided.');
} else if (projectSysIDs.length !== projectDates.length) {
    gs.print('The number of sys_ids does not match the number of dates.');
} else {
    // Create a GlideRecord instance for the 'pm_project' table
    for (var i = 0; i < projectSysIDs.length; i++) {
        var sysId = projectSysIDs[i];
        var dateStr = projectDates[i] + ' 00:00:00'; // concatenate date with time set to 00:00:00
        //gs.print('dateStr: ' + dateStr);
        gs.print('Original date string: ' + dateStr);

        // Create a GlideDateTime object
        var dateTime = new GlideDateTime();
        dateTime.setValue(dateStr);

        // Convert the GlideDateTime object to UTC
        gs.print('dateTime in UTC: ' + dateTime.getValue());

        var gr = new GlideRecord('pm_project');
        if (gr.get(sysId)) {
            // Print original date value for debugging purposes
            gs.print('Updating Project: ' + gr.getValue('short_description'));
            gs.print('Old Date Value: ' + gr.getValue('u_project_won_date'));

            // Set the custom date field with the new date-time value
            gr.setValue('u_project_won_date', dateTime);

            // Print the new value for debugging purposes
            gs.print('New Date Value: ' + gr.getValue('u_project_won_date'));

            // Update the record in the database
            gr.update();
        } else {
            gs.print('Project with sys_id ' + sysId + ' not found.');
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@ashwinipingle 

try this

when you use setValue() on GlideDateTime, it sets value in UTC and no need to convert to timezone

// Define the list of project sys_ids and corresponding dates
var projectSysIDs = [
    'bb27e5b8c3f34250383514a4e40131e5'
]; // replace with your actual sys_ids

var projectDates = [
    '2024-11-12'
]; // replace with your actual dates
//yyyy-mm-dd
if (projectSysIDs.length === 0) {
    gs.print('No sys_id values provided.');
} else if (projectSysIDs.length !== projectDates.length) {
    gs.print('The number of sys_ids does not match the number of dates.');
} else {
    // Create a GlideRecord instance for the 'pm_project' table
    for (var i = 0; i < projectSysIDs.length; i++) {
        var sysId = projectSysIDs[i];
        var dateStr = projectDates[i] + ' 00:00:00'; // concatenate date with time set to 00:00:00
        //gs.print('dateStr: ' + dateStr);
        gs.print('Original date string: ' + dateStr);

        // Create a GlideDateTime object
        var dateTime = new GlideDateTime();
        dateTime.setValue(dateStr);

        // Convert the GlideDateTime object to UTC
        gs.print('dateTime in UTC: ' + dateTime.getValue());

        var gr = new GlideRecord('pm_project');
        if (gr.get(sysId)) {
            // Print original date value for debugging purposes
            gs.print('Updating Project: ' + gr.getValue('short_description'));
            gs.print('Old Date Value: ' + gr.getValue('u_project_won_date'));

            // Set the custom date field with the new date-time value
            gr.setValue('u_project_won_date', dateTime);

            // Print the new value for debugging purposes
            gs.print('New Date Value: ' + gr.getValue('u_project_won_date'));

            // Update the record in the database
            gr.update();
        } else {
            gs.print('Project with sys_id ' + sysId + ' not found.');
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks for your response. This further changed the date as well.

The output of program shows as below

Original date string: 2024-11-17 00:00:00
dateTime in UTC: 2024-11-17 00:00:00
Updating Project: H125 HM2 clutch - steel disc
Old Date Value:
New Date Value: 2024-11-17 00:00:00
sn_invst_pln (CostPlanHelper): Cost plan availability for task with sys_id 02f737dcc3711e50383514a4e4013105 is false

 

However on the list view, we see as below

ashwinipingle_0-1736506314983.png

 

@ashwinipingle 

in database it's stored in GMT/UTC

But it's rendered based on timezone of logged in user or the user preferences

That's OOB behavior

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

My earlier code was working fine, it shows an hh that is 00 on the project record and 08 in the program output!!! Thanks for your help.