- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
Hi All,
I have a requirement where I need to fetch only Dates and not time from the OOB field 'Due Date' on SCTASK and update it on newly created Due Date field which is of type date. Please help me in writing the script to achieve this.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
something like this
// Background script to copy date-only from OOB due_date to u_new_due_date on sc_task
var gr = new GlideRecord('sc_task');
gr.addNotNullQuery('due_date');
gr.query();
while (gr.next()) {
var gdt = new GlideDateTime(gr.due_date);
var gd = gdt.getLocalDate();
gr.u_new_due_date = gd.toString();
gr.update();
count++;
gs.info('Updated sc_task ' + gr.number + ': ' + gr.due_date + ' -> ' + gr.u_new_due_date);
}
gs.ifno('Processed ' + count + ' tasks');
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago - last edited 21 hours ago
please refer the below line of code
var gdt = new GlideDateTime(pass your date/time field from the SCTask); // your Date/Time field
please take Reference from the below right now its for the one SCTASK you adjust this as per your requirement.
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
something like this
// Background script to copy date-only from OOB due_date to u_new_due_date on sc_task
var gr = new GlideRecord('sc_task');
gr.addNotNullQuery('due_date');
gr.query();
while (gr.next()) {
var gdt = new GlideDateTime(gr.due_date);
var gd = gdt.getLocalDate();
gr.u_new_due_date = gd.toString();
gr.update();
count++;
gs.info('Updated sc_task ' + gr.number + ': ' + gr.due_date + ' -> ' + gr.u_new_due_date);
}
gs.ifno('Processed ' + count + ' tasks');
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
Hi @ShubhankarK9612 ,
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
21 hours ago
Hi @ShubhankarK9612 ,
To copy only the date portion (without time) from the OOB Due Date field on SC Task (sc_task) to a newly created field of type Date, you can use the following background script.
Since the target field is of type Date, using getDate() ensures that only the date value is extracted and stored correctly, without any time component.
var gr = new GlideRecord('sc_task');
gr.addNotNullQuery('due_date');
gr.query();
var count = 0;
while (gr.next()) {
// Extract only the DATE part
var gd = gr.due_date.getDate();
// Set Date field directly (no toString)
gr.u_new_due_date = gd;
gr.update();
count++;
}
gs.info('Total records updated: ' + count);Explanation
- Queries all SC Task records where Due Date is not null
- Uses getDate() to extract only the date part from the Date/Time field
- Updates the custom Date field with the extracted value
- Ensures no time component is stored in the new field
If this solution helped resolve your issue, please mark it as the accepted solution and give it a thumbs up 👍
Thank you.
