Extract the date from GlideDateTime()

carlocsa
Kilo Expert

Hello Experts!

How do I extract or use the date from GlideDateTime()?

Thank you!

Carlo

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please try,



var sdt = new GlideDateTime();


var dt = sdt.getDisplayValue().split(' ')[0];


gs.print(dt);


View solution in original post

5 REPLIES 5

Shishir Srivast
Mega Sage

Please try,



var sdt = new GlideDateTime();


var dt = sdt.getDisplayValue().split(' ')[0];


gs.print(dt);


Tristan Cricket
Kilo Contributor

var rightnow = new GlideDateTime();

var rightnow_date = rightnow.getLocalDate();

This works better than the original answer. If you're running this as a user, gdt.getLocalDate() will give you a string of yyyy-mm-dd, whereas gdt.getDisplayValue().split(' ')[0]; will return it with the user's preferences, which may not be what you expected. 

 

If you have to do a compare, using the yyyy-dd-mm string will let you compare greater than/less than comparisons directly on a string value, and get the result you expect.

Karthik Buska
Tera Contributor

var gdt = new GlideDateTime();

var dt = gdt.getDate();

gs.print(dt);

 

 

This would be more efficient as we can avoid using splits and typecasting