Script for fetching only date from OOB field ''Due Date' and updating it into new field Due Date

ShubhankarK9612
Giga Contributor

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.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@ShubhankarK9612 

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! 🙏

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

View solution in original post

5 REPLIES 5

Sanjay191
Kilo Patron
Hi @ShubhankarK9612 

please refer the below line of code

var
gdt = new GlideDateTime(pass your date/time field from the SCTask); // your Date/Time field
var dateOnly = gdt.getDate();
gs.print(dateOnly); 
 
please take Reference from the below right now its for the one SCTASK you adjust this as per your requirement.
var gr = new GlideRecord('sc_task');
if (gr.get('2325583c2b0e3e50b882ff22f291bf3f')) {
    var gdt = new GlideDateTime(gr.due_date);
    var dateOnly = gdt.getDate();
    gs.print(dateOnly);
}


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Ankur Bawiskar
Tera Patron

@ShubhankarK9612 

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! 🙏

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

DiveshTyagi
Tera Contributor

Hi @ShubhankarK9612 ,

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);
 
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Aditya40
Tera Expert

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.