How to store today's date/time in a variable and print it

Helan
Tera Contributor

Hello,

How to store today's date/time in a variable and print it ?

1 ACCEPTED SOLUTION

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @Helan Try below script I try in background script 

var dt = new GlideDateTime(); 
gs.print("Current Date and Time: " +dt); 

Screenshot 2025-05-24 at 8.40.00 PM.png

View solution in original post

8 REPLIES 8

Robert H
Mega Sage

Hello @Helan ,

 

Your question is ambiguous.

Are you talking about a JavaScript variable in a script? If so, is is a Client Script or a server side one?

Or are you referring to a Variable (question) on a Catalog Item?

 

Randomly assuming you are talking about a server script, here is the answer:

var now = new GlideDateTime();
gs.info(now.getDisplayValue());

 

Regards,

Robert

@Helan What is the concern with this answer?

Helan
Tera Contributor
  • Thanks for your help @Robert H @! Your script worked,but it gave yesterday’s date. I needed the current date and time, so I accepted the other answer. Appreciate your support

 

 

Hello @Helan ,

 

Actually, all solutions presented here will give you the current date and time.

The difference is the time zone that this time is interpreted in.

 

My solution prints the date/time as per the time zone selected in the current user's profile. If you have not set your time zone correctly e.g. it's set to US/Pacific but you're actually located in India you will sometimes see yesterday's date. If you update your time zone setting to IST then the above code will print your exact local date and time.

 

The other solutions print the date/time as per the UTC time zone. This is IST -5:30 hours. So if you use that solution in the very early morning it will also print yesterday's date.

 

Regards,

Robert