How do you convert a time to the correct timezone?

e_wilber
Tera Guru

I have a UI page that has a date/time field that returns the value in format: 2023-07-07 11:13

 

What I need to do is take a look at the time entered and assume it's sent in a specific time zone, and then check if the selected time is inSchedule.

 

This is being called by a script include so we are passing in the time in format shown above AND a timezone such as US/Central or US/Eastern.

 

What I am trying to do is take the time that was input on the form and set that as the time for the selected timezone rather than the user's timezone and it doesn't look like that's happening in my code below. It looks like it's converting my 11:13 time (in Central) to 06:13 instead and I'm not able to calculate which schedule the time is within.


Am I missing a step?

 

var startTime = '2023-07-07 11:13';
var timezone = 'US/Central';
 
var time = new GlideDateTime(startTime);
gs.print(time.getDisplayValue()); // output: 07/07/2023 04:13:00
time.setTimeZone(timezone); // output: 07/07/2023 06:13:00
gs.print(time.getDisplayValue());
var inShift = '';
    var sched = new GlideSchedule('dde09e971b9b291041c20e91604bcb1d'); // Shift 1
    if(sched.isInSchedule(new GlideDateTime(time.getDisplayValue()))){
      inShift = 'dde09e971b9b291041c20e91604bcb1d';
    }

    var sched = new GlideSchedule('dcf5d6db1b9b291041c20e91604bcb29'); // Shift 2
    if(sched.isInSchedule(new GlideDateTime(time.getDisplayValue()))){
      inShift = 'dcf5d6db1b9b291041c20e91604bcb29';
    }  
 
1 REPLY 1

Syed30
Tera Expert

HI @e_wilber 

In your code, it seems that you are correctly setting the timezone for the GlideDateTime object. But, the issue lies in the way you're using the getDisplayValue() method. The getDisplayValue() method returns the date and time value in the user's timezone, not the specified timezone.

you should use the getNumericValue() method instead. hope this helps.

Also see these links Convert date to another time zone

Converting specified Time Date