- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 01:35 AM
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
*** 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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:05 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:05 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:58 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:58 AM
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.