Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
Hi,
I am currently trying to develop a service that should generate a number consisting of a date shorened to yymmddhhmm.
When writing the bellow code in javascript (nodejs)
const date = new Date();
console.log("SSNDate: "+date.toLocaleString("sv-SE", { timeZone: "Europe/Stockholm" }).slice(2).replace(/\s|\.|:|-/g,""));
I get
230929090129
With this code:
dateFormat(date, "YYMMDDHHmm")
I get
23092720756
I would also like my code to take locale and timeZone into account, how is that done?
Calling new Date, I get what I understand looks like a unix timestamp:
1695974346423
To summarize:
- How to format a date according to YYMMDDHHmmss?
- How to take a certain timezone into account?
- What is date returning?
- How to take timeZone and locale into account, rhinos implementation does not seem to do it the standard way with toLocaleString.
- What dateStrings can be used with dateFormat, and how to check that?
Regards
Oskar Berntorp
Solved! Go to Solution.
Yes, in Thingworx Date() returns a Unix timestamp.
To get the usual output, please use the toString() method to convert it
/VR
You can get user TimeZone Offset using the Expression Function in Mashup Runtime
timevalue = new Date();
Output = timevalue.getTimezoneOffset();
In script/service, you can add timezone offset minutes to dateTime value to adjust the timezone difference
dateAddMinutes(new Date(), (timeZoneOffset * -1))
Additional Reference - Solved: Re: get time service return wrong time - PTC Community
/VR
Hi @oskarberntorp ,
The format you supply to dateFormat is this one http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
This is mentioned in this article: https://www.ptc.com/en/support/article/CS282344
Timezones can't be used server-side in ThingWorx to format the date in a specific Timezone.
You can though apply Velkumar's approach to modify the date first, then translate it, but you can't say result= dateFormat (date,"format", timezone).
I'm not sure what the question around "What is date returning?" is about - can you clarify?
There are two types of date in ThingWorx.
Hi,
When calling the Date constructor, a bunch of letters are returned, this is not according to how it is normally done in JavaScript, I asume the date constructor in Thingworx rhino implementation of JavaScript is returning a unix timestamp?
let date = new Date() -> 1695974346423 <- This is not a ordinary date (Date() in JavaScript returns Fri Sep 29 2023 14:42:36 GMT+0200 (Central European Summer Time)), buth rather looks like a unix timestamp?
I have compared it to Date.now and it definitely has the same length.
Yes, in Thingworx Date() returns a Unix timestamp.
To get the usual output, please use the toString() method to convert it
/VR