Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
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?
Solved! Go to Solution.
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;
var utc = new Date();
var result = new Date(utc.getTime() + 12*60*60000);
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.
var utc = new Date();
var result = new Date(utc.getTime() + 13*60*60000);
So, there is no automated way? +13 works only for 6 months of the year, and the change-over days are different each year.
"if...else.." can be added to js, such as:
var utc = new Date();
var month = utc.getMonth();
var result;
if (month<=6) {
result = new Date(utc.getTime() + 12*60*60000);
} else {
result = new Date(utc.getTime() + 13*60*60000);
}
I understand, thanks.
But I would need to code for the following rules, for which I think I use a look-up table, and will require more code than a straightforward if, then, else.
"Daylight saving starts each year at 2 am on the last Sunday in September, and ends at 3 am on the first Sunday in April."
@Kuroisan,
Not sure exactly the issue you are trying to address. If you are looking to get the information in the JavaScript which is running in the browser have you considered asking the Browser?
URL with information on different options: https://www.w3docs.com/snippets/javascript/how-to-get-the-client-timezone-offset-in-javascript.html
let split = new Date().toString().split(" ");
let timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1].slice(0,4);
console.log(timeZoneFormatted);
This should return "Standard Time"
If you provide a string in the date function to set the date into daylight savings time you should get "Daylight Time".
The context here is Javascript running on the server, the Thingworx server. There is no browser, other than that I am editing the code through a browser.
The main issue, as @JE says below, the following code is not fully functional on Thingworx.
et text = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
The timezone parameter is not respected.
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.
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;
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.
* 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.