Why getDate() is not working properly?

Vasu ch
Kilo Sage

Hi all,

I've a requirement where I have to capture the record created date in another field without time attached. Simply, in OOB we have Created field which stores record created date and time as well. But I just need to capture the record created date without time in another field.

I've created the following script to capture the date in new field(u_created_date) which is a Date type field.

while(gr.next()){
    var caseCreationDate = gr.getValue("sys_created_on");
    var gd = new GlideDateTime(caseCreationDate);
    gr.setValue("u_created_date",gd.getDate());
    gr.update();
}

 

On executing the above script only some fields are giving me the right date. Some fields are giving wrong date.

 

find_real_file.png

Why it is happening like that?

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

possibly because of timezone

try this

use getLocalDate() function

while(gr.next()){
    var caseCreationDate = gr.getValue("sys_created_on");
    var gd = new GlideDateTime(caseCreationDate);
    gr.setValue("u_created_date",gd.getLocalDate()));
    gr.update();
}

Regards
Ankur

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

Hi Ankur,

Thanks for the response. I tried getLocalDate() but still getting some mismatches.

find_real_file.png

Hi,

did you try to print value for sys_created_on?

try this

while(gr.next()){
    var caseCreationDate = gr.sys_created_on;
    var gd = new GlideDateTime(caseCreationDate);
    gr.setValue("u_created_date",gd.getLocalDate()));
    gr.update();
}

Regards
Ankur

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

I tried Ankur. But still getting same issues