I am not getting time in dubai time zone

chandan86patra
Tera Contributor

Hi All,

I am not getting time in dubai time zone :_

var gr = "20231130T060000Z"; 
var formattedDate = gr.substring(0, 4) + '-' + gr.substring(4, 6) + '-' + gr.substring(6, 😎 + ' ' + gr.substring(9, 11) + ':' + gr.substring(11, 13) + ':' + gr.substring(13, 15);
var gdt = new GlideDateTime(formattedDate);
var hours = gdt.getHours();
var minutes = gdt.getMinutes();
var seconds = gdt.getSeconds();
gs.addErrorMessage("Formatted Date: " + hours);
gs.addErrorMessage("GlideDateTime Object: " + minutes);
var formattedTime = hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
var timeOnly = formattedTime;
gs.addErrorMessage("Extracted time: " + timeOnly); // To log the result for verification

 

This script is not working properly , Can anyone modify the script

 

Thanks,

Chandan 

 

1 ACCEPTED SOLUTION

Simon Christens
Kilo Sage

You need to understand the user for GlideDateTime.

When you parse a date / time into GDT then its set as UTC time - ALWAYS.

All date and date time fields in SN is converted into UTC time and depending on your user timezone the display of the fields are local times.

So depending on how you want to store the date time you need to use the same formats.

Example of getting local times out of a GDT:

var time = "20231130T060000Z".replace('T', ' ').replace('Z','');
var gdt = new GlideDateTime();
gdt.setDisplayValue(time, 'yyyyMMdd HHmmss'); //We set the display value if its a local time input - else we use gdt.setValueUTC(<date time input>, <which format it in>)

gs.info(gdt.getValue()); //UTC time
gs.info(gdt.getDisplayValueInternal());//Local time server formated

//Switch between getValue() and getDisplayValueInternal() to change local time with UTC time
var timePartAsArray = gdt.getDisplayValueInternal().split(' ')[1].split(':');

gs.info('Hours ' + timePartAsArray[0]);
gs.info(('Minutes ' + timePartAsArray[1]));
gs.info('Seconds ' + timePartAsArray[2]);

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@chandan86patra 

what's your business requirement here?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I require Only time but in dubai time  zone because i want to display or hide the button . Only 8am to 6 pm  button should display else hide the button in portal 

@chandan86patra 

you can use UI action condition and use GlideSchedule

you might already be having a schedule for this. simply use this

How to Hide a Button based on Business/Working Hours of the Assignment Group. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@chandan86patra 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader