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

Shane J
Tera Guru

Well I found a now 4 yr old non-Community post that led me to a solution.

https://neuralworkz.wordpress.com/2018/03/13/auto-refresh-data-in-servicenow-widget/

In the Client Controller I can use:

    var now = new Date();
    $scope.now = now.toLocaleDateString() + " " + now.toLocaleTimeString().substring(0,5) + now.toLocaleTimeString().substring(8);

And then use {{now}} in the HTML to show the system time, outside of whatever SN is trying to show.

Actual Local Date/Time is: 7/27/2022 1:25:PM