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

Hi,

you can send the values to script include as it is. Also send the logged in user's date format to Script include function

in client script you can use this to get the user's date format

var format = g_user_date_format; // send this to ajax

Then inside script include convert that user given format to system one and get the duration

Can you share your script include code

Regards
Ankur

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

PrashantLearnIT
Giga Sage

Hi Ravi,

Please try to use the below logic, if it helps you.

var date= new GlideDate();//replace new GlideDate() with the incoming date
var date2 = date.getByFormat('yyyy/MM/dd');//now use the date2 to set the field value

Prashant

If my answer helped you in any way, please mark this answer as helpful and correct.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

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.

Thank you Muhammad,

it is giving expected output. but the substract method is not working with correct date formats.

find_real_file.png

You should return diff.getDisplayValue(); which will return actual duration.