getByFormat is not working as expected

xavifrancis
Kilo Expert

The below script is not working as expected.

The variable longDateTime should print value as "December 31, 2019" but it is printing as "December 31, 2020".

var current = new GlideRecord('proc_po');
current.addEncodedQuery('u_amat_poSTARTSWITH4400475274');
current.query();
if(current.next()){
var gDateTime = new GlideDateTime(current.po_date.getDisplayValue());
var gDate = gDateTime.getDate();
gs.print(gDate); // prints - 2019-12-31 - correct value
var longDateTime = gDate.getByFormat("MMMM dd, YYYY");
gs.print(longDateTime); // prints - December 31, 2020 - wrong year
}

Need help to fix this.

Thanks,

Francis Xavier K.

1 ACCEPTED SOLUTION

ohk got the issue. try now , i have tested it. 

 

 


var a = '2019-12-31 08:00:00';
var gDateTime = new GlideDateTime(a);
var gDate = gDateTime.getDate();
gs.print(gDate); // prints - 2019-12-31 - correct value
var longDateTime = gDate.getByFormat("MMMM d, yyyy");
gs.print(longDateTime); // prints - December 31, 2020 - wrong year

 

If my answer helped you, kindly mark it as correct and helpful.

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Francis,

can you print the gDateTime value?

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

below is the output we are getting for gDateTime.

2019-12-31 13:30:00

Harsh Vardhan
Giga Patron

try now. 

 

var current = new GlideRecord('proc_po');
current.addEncodedQuery('u_amat_poSTARTSWITH4400475274');
current.query();
if(current.next()){
var gDateTime = new GlideDateTime(current.po_date);

gDateTime.getDisplayValueInternal();

var gDate = gDateTime.getDate();
gs.print(gDate); // prints - 2019-12-31 - correct value
var longDateTime = gDate.getByFormat("MMMM dd, YYYY");
gs.print(longDateTime); // prints - December 31, 2020 - wrong year
}

It gives the same result.

Check the below simplified script:

var a = '2019-12-31 08:00:00';
var gDateTime = new GlideDateTime(a);
var gDate = gDateTime.getDate();
gs.print(gDate); // prints - 2019-12-31 - correct value
var longDateTime = gDate.getByFormat("MMMM dd, YYYY");
gs.print(longDateTime); // prints - December 31, 2020 - wrong year

which gives the same result.