how to get today date in servicenow notification mail script

RamuN
Tera Contributor

My query is how it is taking different todays date 

 

var todayDate = new GlideDateTime();
todayDate.setDisplayValue(todayDate.getDate().getByFormat('yyyy-MM-dd') + ' 00:00:00');
gs.info("Adjusted todayDate: " + todayDate);
 
Output:
logs created on 15-06-2024 at 6:00 

Adjusted todayDate: 2024-06-13 14:00:00

 

and

 

Output:

logs created on 15-06-2024  at 17:41

Adjusted todayDate: 2024-06-14 14:00:00

 

please advice how to proceed to calculate todays date

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @RamuN ,

 

The inconsistent Adjusted todayDate values in your logs seems related to time zone differences and the behavior of the GlideDateTime methods you are using.

 

The setDisplayValue method sets the date and time according to the user's time zone, which might be causing the discrepancy, you should use setValue which basically converts the time in UTC and then convert it based on users time zone.

 

You can use the below snippet for the same-

 

var gdt = new GlideDateTime();
var midnightUtc = new GlideDateTime(gdt.getDate().getByFormat('yyyy-MM-dd') + ' 00:00:00'); // UTC midnight
midnightUtc.setTZ(gdt.getTZ()); // Convert to user's time zone

gs.info("Adjusted todayDate: " + midnightUtc);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @RamuN ,

 

The inconsistent Adjusted todayDate values in your logs seems related to time zone differences and the behavior of the GlideDateTime methods you are using.

 

The setDisplayValue method sets the date and time according to the user's time zone, which might be causing the discrepancy, you should use setValue which basically converts the time in UTC and then convert it based on users time zone.

 

You can use the below snippet for the same-

 

var gdt = new GlideDateTime();
var midnightUtc = new GlideDateTime(gdt.getDate().getByFormat('yyyy-MM-dd') + ' 00:00:00'); // UTC midnight
midnightUtc.setTZ(gdt.getTZ()); // Convert to user's time zone

gs.info("Adjusted todayDate: " + midnightUtc);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

used below 

 

 var tz = gs.getSession().getTimeZone();
        var todayDate = new GlideDateTime();
        todayDate.setTZ(tz);
 
thank you