getByFormat is not working as expected!?

Ravi Chandra_K
Kilo Patron
Kilo Patron

find_real_file.png

the expected output is 2005-01-31 but I'm getting 0036-06-27. how to resolve this?

 

find_real_file.png

1 ACCEPTED SOLUTION

Muhammad Khan
Mega Sage
Mega Sage

Hi Kukunooru,

 

Use the following in background script;

var gd = new GlideDateTime();
gd.setValue('31-03-2005');
gs.print(gd.getDate());
*** Script: 2005-03-31

Hopefully, this will resolve your query.

View solution in original post

20 REPLIES 20

actually there seems to be some issue with setValue.

while it worked for the above date, if we try the same with date value less than 12, it is writing YYYY-dd-MM instead of YYYY-MM-dd.

example - 

var gd = new GlideDateTime();
gd.setValue('03-01-2008');
    gs.info(gd);
var start = gd.getDate(); 

it is giving 2008-03-01 00:00:00 instead of 2008-01-03.

@Kukunooru Ravi Chandra 

Check my above response

Regards
Ankur

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

thanks for taking time to debug Ankur. Now this is almost giving me correct answer when date selected is above 12. and incorrect answer when date is below 12 (because setValue not setting proper value)

find_real_file.png

 

getting correct

find_real_file.png

 

getting incorrect

 

find_real_file.png

Hi Ankur! it is working now.

instead of setValue(), I have used setDisplayValue()

Thanks for your help.

It is expected behavior as you can see in the API documentation below.

find_real_file.png

So try using gd.setValueUTC('2-1-2008', 'dd-MM-yyyy')