Need Date and Time 12 hours ago

I am trying to do 2 things:
Get the current date for one section (Shift 1)
Get the date and time 12 hours ago (from previous shift)

To get current date/time I have used this:
[[month]]/[[day]]/[[year]] [[hours]]:[[minutes]] [[Now.toLocaleTimeString().slice(-2)]]

I am a newbie at JavaScript stuff

Hi!

You are one the right track here. It might be helpful to use a variable here representing the current date/time. Then you can use the same expression for both shifts.

Create a global variable called dateTime, which we will reuse for both expressions.

Set variable dateTime to [[Now]]
Set text on widget currentShift to [[dateTime.getMonth()]]/[[dateTime.getDate()]]/[[dateTime.getFullYear()]] [[dateTime.getHours()]]:[[dateTime.getMinutes()]] [[dateTime.toLocaleString().slice(-2)]]

set variable dateTime to [[Now.addHours(-12)]]
Set text on widget previousShift to [[dateTime.getMonth()]]/[[dateTime.getDate()]]/[[dateTime.getFullYear()]] [[dateTime.getHours()]]:[[dateTime.getMinutes()]] [[dateTime.toLocaleString().slice(-2)]]

I haven’t actually tried this, but it should work.

[Edit] Since you’ll want a leading zero on minutes, so 10:07 doesn’t end up 10:7, change the minutes expression to

[['0'.concat(dateTime.getMinutes()).slice(-2)]]

This prepends a zero onto the current minutes and takes the last two characters, so 7 ends being the last two characters of 07, and 27 ends up being the last two characters of 027.

It displayed NaN Nan Nan Nan

I created a Global Variable called dateTime and left value empty

On the page I created an interaction OnPageLoad
set Text currentShift to your info
than
set Text previousShift to your other numbers

Hi!

You need to set the global variable first, before each Set Text command.

I must be dense. This is what I did.
39%20PM

And this is what I got
33%20PM

You are on the right track. But you need to set dateTime again before the second Set Text, this time setting it to 12 hours ago. Look again at my code sample.

Note that if you want to set the dateTime variable to a specific date and time (rather than the current date and time), you would use this expression, here setting it to July 7 2016 at 12:20 pm:

set variable dateTime to [[ (Date.parse('7/7/2016 12:20 pm' ) + 0).addHours(0) ]]

It’s a little convoluted, but here are the details.

  • Date.parse() returns the date/time as a number of milliseconds. For whatever reason, Axure isn’t returning this as a number like Javascript does, so we force it to be evaluated as a number by adding 0.
  • However, we need dateTime to be a date object, not an integer. The addHours() function returns a new date object, and since we are adding 0 hours, the date object contains the time entered.

There is probably another way to do this, but this is what I’ve found works.