cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

How to apply dateFormat YYMMDDHHmmss

oskarberntorp
13-Aquamarine

How to apply dateFormat YYMMDDHHmmss

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

 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions

Hi @oskarberntorp  

 

Yes, in Thingworx Date() returns a Unix timestamp. 

 

To get the usual output, please use the toString() method to convert it 

Velkumar_0-1696307954938.png

 

/VR

 

 

View solution in original post

4 REPLIES 4

Hi @oskarberntorp 

 

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.

  1. The data ThingWorx stores in its DB - always UTC
  2. The data that ThingWorx displays to you - always converted in Composer and Runtime taking into consideration your browser's OS timezone - can't deactivate this. What you can do only in Runtime, is to substract/add minutes (like in Velkumar's approach) to display it effectively in another timezone.

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.

Hi @oskarberntorp  

 

Yes, in Thingworx Date() returns a Unix timestamp. 

 

To get the usual output, please use the toString() method to convert it 

Velkumar_0-1696307954938.png

 

/VR

 

 

Announcements


Top Tags