How to create a date object from Unix Epoch or a given date?


#1

Hi,

The main question is:
How can I create a date object from a given date?

Context:
I am trying to do some calculations with a date given in the following format: DD/MM/YYYY .
What I am trying to do is to convert the given date to a date object so I could use the existing date methods (addMonths(X) / addDays(Y) ), however I could not find a method to create a date object providing the date.
The closest method I could find was Date.UTC() , which gives me the epoch representation of the given date, but I don’t know how to create a date object from it.

Current proposed step-by-step:

  1. get date in epoch format: [[ Date.UTC(LVAR1.substr(6,4), LVAR1.substr(3,2)-1,LVAR1.substr(0,2))
  2. convert epoch to date: ???
  3. do necessary calculations: addMonths(-2)
  4. convert to locale: toLocaleDateString(“pt-BR”) ]]
  5. profit!

P.S.:
I am sorry if this information is available somewhere else, I didn’t find it in https://docs.axure.com/axure-rp/reference/math-functions-expressions/#date-and-time neither in any other topic of this forum.

Thanks.
Regards,
Luiz Taboada


#2

Hi Luiz!

This post does the string conversion

Note that the end goal of this post was to get it into millisecond form. Here you just want the string, so use this expression:

set usDate to [[ d.substring(d.indexOf('/') + 1, d.lastIndexOf('/')).concat('/').concat(d.substring(0,d.indexOf('/'))).concat('/').concat(d.substring(d.lastIndexOf('/') + 1, d.length)) ]]

…assuming d is the local variable referring to your date in DD/MM/YYYY format.


#3

Hi Joseph,

Thank you for your prompt response.
The expressions that you provided will give me either the date in millisecond form or in string form, which I have already managed to get with less elegant expressions, but I am looking for the date object so I could manipulate it easier with the Date methods (e.g.: addMonths(-2)).

How can we instantiate a Date object from either of these two formats?

I am looking for this information:

Regards,
Luiz


#4

Hi Joseph,

I found the answer to my question in the example you provided in this topic: Filtering Repeater by Date Range

What I needed to do was to cast the date in millis (epoch format) using a “+0” at the end and then I could manipulate it with the Date methods for calculations that I was looking for.
The final result was something like this:

[[ (LVAR1+0).addMonths(10).toLocaleDateString(“pt-BR”) ]]
where LVAR1 was the date in epoch format (time in millis from 1970).

Thank you very much for your help!


closed #5

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.