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

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.

Thank you Harshvardhan...

It works fine now 

for future reference, you can refer the below link for correct date format string.

 

https://help.talend.com/reader/3zI67zZ9kaoTVCjNoXuEyw/YHc8JcQYJ7mWCehcQRTEIw

 

 

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

 

Jorn van Beek
Tera Guru

put the YYYY as yyyyy (from upper case to lower case).

the upper case format gives back 'year of week', 31 december could be in week 1 of the next year.

the lower case format gives back 'current year', what you are looking for.