How can I get user's local DateTime without ServiceNow imposing their Time Zone setting?

Shane J
Tera Guru

I'm trying to put together a pretty simple widget to show on the Portal, to show the logged in user:

  • their current Time Zone
  • what DateTime SN thinks is 'current'
  • what their actual current DateTime is

I can't get the last one to work however.  It seems like no matter what I try, SN enforces the user's Time Zone on what is being displayed.  I've used Date() outside of SN in another code playground site and it sends my local DateTime correctly.

Results:

Your Time Zone is set to: US/Pacific

Current Date/Time (using the Time Zone) is: 2022-07-26 12:38:40

Your actual Local time is: Tue Jul 26 2022 12:38:40 GMT-0700 (PDT)

TEST: 12:38:40 PM

TEST should be showing 2:38:40 pm .  Whenever I change the TZ it just changes to reflect that.

 

Here's what I have currently:

Widget HTML body:

<div>
  <p>Your Time Zone is set to: {{::data.tz}}</p>
  <p>Current Date/Time (using the Time Zone) is: {{::data.now}}</p>
  <p>Your actual Local time is: {{::data.local}}</p>
  <p>TEST: {{::data.test}}</p>
</div>

 

Widget Server:

(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */
    var session = gs.getSession();
    var zoneName = session.getTimeZoneName();
    data.tz = zoneName;

    var gdt = new GlideDateTime();
    data.now = gdt.getDisplayValue();

    //data.local = (new GlideDateTime()).getLocalTime().getByFormat('hh:mm:ss a');
    //data.local = new Date().toLocaleTimeString();
    data.local = new Date().toString();

    data.test = moment().format('LTS');

})();

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

@Shane J hello,

can you elaborate more on third point what is exactly needed over there to be printed?

I want the actual computer DateTime of the user who is going to the Portal.  We're basically trying to get people to set their Time Zone if there's a discrepancy between what SN thinks is 'local' and their actual.

@Shane J  okay so basically the TZ of the logged in user right ?

I really don't care about the TZ, just the DateTime that the user see's on the bottom right of their (Windows, I don't know where it is on Mac) PC. 😉