Format Date GlideDateTime to dd.mm.yyyy ?

Hendrik3
Kilo Contributor

I am running a server side script which calculates the last day of the month.

var gdt = new GlideDateTime();
gdt.addMonthsUTC(-1);
var month = gdt.getMonthUTC();
var year = gdt.getYearUTC();
var d = new Date(year, month + 1, 0);
gs.print(d); // last day of month

My current output looks as follows:

Wed Feb 28 2018 00:00:00 GMT-0800 (PST)

What I would love to receive is:
28.02.2018

I would love to avoid using stupid replace scripts or regex. There must be an easy way.

I have played with gdt.setDisplayValue  without success. 

Any help is upvoted

5 REPLIES 5

tushar_sri
Tera Contributor

Formatting GlideDateTime to dd-MM-yyyy (Scoped Applications)

 

If you’re working in a scoped application and need to format a date into dd-MM-yyyy, you can use GlideDateTime and split the value accordingly.

 

🔹 Important Note:

To manipulate the date, always use .getValue() before splitting.

 

💡 Example 1: Using GlideDateTime

var gdt = new GlideDateTime();
var today = gdt.getLocalDate().getValue();
gs.print(today);
var parts = today.split('-');
var ddMMyyyy = parts[2] + '-' + parts[1] + '-' + parts[0];
gs.print(ddMMyyyy);



💡 Example 2: Using GlideDate
var
gd = new GlideDate().getValue();
gs.print(gd);
var parts = gd.split('-');
var ddMMyyyy = parts[2] + '-' + parts[1] + '-' + parts[0];
gs.print(ddMMyyyy);



If you find my solution helpful then hit the like button.