Skip to main content
1-Visitor
March 1, 2022
Solved

How to convert a UTC Date to a local time string?

  • March 1, 2022
  • 3 replies
  • 11880 views

When I get the current time with,

var currentTime = new Date();

I get a datetime in UTC, which is fine, and the correct UTC time. But I need to convert that time to local New Zealand time is order to submit a correct request to the Process Historian API.

 

How can a JavaScript Date be converted to a local time string?

 

Best answer by Kuroisan

I ended up writing a Service called UTCtoNZT to do the conversions. It takes a UTC DateTime as an input, and returns a DateTime in New Zealand Time.

 

var endDate = ["3 Apr 2022 03:00:00 +1300","2 Apr 2023 03:00:00 +1300",
"7 Apr 2024 03:00:00 +1300","6 Apr 2025 03:00:00 +1300",
"5 Apr 2026 03:00:00 +1300","4 Apr 2027 03:00:00 +1300"];

var startDate = ["25 Sep 2022 02:00:00 +1200","24 Sep 2023 02:00:00 +1200",
"29 Sep 2024 02:00:00 +1200", "28 Sep 2025 02:00:00 +1200",
"27 Sep 2026 02:00:00 +1200", "26 Sep 2027 02:00:00 +1200"];

const yearOffset = 2022; // array index 0 is year 2022.
var year = dateTime.getUTCFullYear();
var i = year - yearOffset;
var endDate_i = new Date(endDate[i]);
var startDate_i = new Date(startDate[i]);
var hourOffset = 0;

if (dateTime < endDate_i || dateTime > startDate_i) {
hourOffset = 13; // + 13 hours
} else {
hourOffset = 12;
}

var NZdateTime = new Date(dateTime.getTime() + hourOffset * 60 * 60000);
var result = NZdateTime;

 

3 replies

17-Peridot
March 1, 2022

var utc = new Date();
var result = new Date(utc.getTime() + 12*60*60000);

Kuroisan1-VisitorAuthor
1-Visitor
March 1, 2022

This gets me close. Is there a way that is can deal with daylight saving time? This returned New Zealand Standard Time, whereas the local time today is New Zealand Daylight Saving Time.

17-Peridot
March 1, 2022

var utc = new Date();
var result = new Date(utc.getTime() + 13*60*60000);

17-Peridot
March 1, 2022

Hello,

 

I have pretty much the exact same issue as you do, saving times in UTC is fine (this is decided in the apache configuration for your TWX instance, and from the information I received while discussing this with a PTC resource it is recommended not to change it).

 

I need to send a local time to a label printing system because it wouldn't make much sense to send the UTC time in that case.

 

I have tried this for example:

let text = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});

But for whatever reason only the "en-US" locale of the toLocaleString seems to work, the timeZone does not. Could this be because running a service is not the same as if a user runs something in the browser?

 

One way we solved it was to have a time offset in minutes like this:

let DateTimeIn = new Date(); // UTC date/time

var myTime = DateTimeIn.getTime() + me.SiteTimeZone * 60000;

 

// here we have the same issue where we need to have the time zone offset, which we set in minutes like "-240" for example for GMT-4(5?)


var DateIn_TZ_Adjusted = new Date(myTime);

var yyyy = DateIn_TZ_Adjusted.getFullYear();
var mon = String(DateIn_TZ_Adjusted.getMonth() + 1).padStart(2, '0');
var dd = String(DateIn_TZ_Adjusted.getDate()).padStart(2, '0');
var hh = String(DateIn_TZ_Adjusted.getHours()).padStart(2, '0');
var mm = String(DateIn_TZ_Adjusted.getMinutes()).padStart(2, '0');
var ss = String(DateIn_TZ_Adjusted.getSeconds()).padStart(2, '0');

// Compose formatted string
var DateAndTimeFormatted = yyyy + "-" + mon + "-" + dd + "T" + hh + ":" + mm + ":" + ss;
result = DateAndTimeFormatted;

 

The rest of this I'm not too sure about, but I think it's just how to format after en-US locale.

 

If anyone has a different way, using only the system time (as our server is local) then please let us know.

Kuroisan1-VisitorAuthorAnswer
1-Visitor
March 4, 2022

I ended up writing a Service called UTCtoNZT to do the conversions. It takes a UTC DateTime as an input, and returns a DateTime in New Zealand Time.

 

var endDate = ["3 Apr 2022 03:00:00 +1300","2 Apr 2023 03:00:00 +1300",
"7 Apr 2024 03:00:00 +1300","6 Apr 2025 03:00:00 +1300",
"5 Apr 2026 03:00:00 +1300","4 Apr 2027 03:00:00 +1300"];

var startDate = ["25 Sep 2022 02:00:00 +1200","24 Sep 2023 02:00:00 +1200",
"29 Sep 2024 02:00:00 +1200", "28 Sep 2025 02:00:00 +1200",
"27 Sep 2026 02:00:00 +1200", "26 Sep 2027 02:00:00 +1200"];

const yearOffset = 2022; // array index 0 is year 2022.
var year = dateTime.getUTCFullYear();
var i = year - yearOffset;
var endDate_i = new Date(endDate[i]);
var startDate_i = new Date(startDate[i]);
var hourOffset = 0;

if (dateTime < endDate_i || dateTime > startDate_i) {
hourOffset = 13; // + 13 hours
} else {
hourOffset = 12;
}

var NZdateTime = new Date(dateTime.getTime() + hourOffset * 60 * 60000);
var result = NZdateTime;

 

17-Peridot
March 8, 2022

Hello,

 

After discussing this with a PTC resource I got one way to do this that could probably work even for daylight savings time (I hope).

 

let params = {
timezoneName: "America/New_York"
};

let offset = Resources["TimezoneServices"].GetOffsetFromTimezone(params);
let offsethrs = offset/100;
let today = new Date();
let datehrs = dateAddHours(today,offsethrs);
var result = datehrs.toLocaleTimeString();

 

Now, I haven't been able to try this out because my TWX doesn't think there is a resource called "TimezoneServices". I'm not sure if this is due to a versioning issue or something else. But I thought it might be worth a shot for you.

 

I am also waiting for a more robust way of doing this as this solution only works for time zones with full hour difference.

I'll try to remember to update this post with any new information I get.

17-Peridot
March 14, 2022

* UPDATE *

 

let params = {
timezoneName: "America/New_York"
};

let offset = Resources["TimezoneServices"].GetOffsetFromTimezone(params);

 

This code does indeed take into account daylight savings. I have confirmed this by checking that it went from -500 to -400 after the change.

So this could be used for this. If needed, you could set the timezone name from the user info or something if you want more modularity. But if you only want the server time, then this works.

 

You can also, if your DB server is local, get the timestamp of the DB.