Javascript for 12 hour clock

Dec 06, 2018

Hi everyone, does anyone know how to display a clock in 12 hour format like the one shown in the image below? 

So far I have only found the javascript for displaying clock in 24 hour format from previous discussions (see attached file)

Hope someone can help me soon! Thank you :D

9 Replies
Jing Her Tan

Hi! Thanks so much for the quick response. I'm not too familiar with java script so I was wondering how do i remove the seconds from the display?

I removed the sec from this line
player.SetVar("12Hour", hour+":"+minute+":"+sec+" "+am_pm);

And also i deleted this part of the code 
var sec = date.getSeconds();
if(sec<10){
sec = "0"+sec;


So now the code looks like this 

var player = GetPlayer();

function date_time() {
var date = new Date();
var am_pm = "AM";
var hour = date.getHours();
if(hour>=12){
am_pm = "PM";
}
if(hour>12){
hour = hour - 12;
}
if(hour<10){
hour = "0"+hour;
}

var minute = date.getMinutes();
if (minute<10){
minute = "0"+minute;
}

player.SetVar("12Hour", hour+":"+minute+am_pm);

}

setInterval(date_time,500);

 
But the seconds appears and disappears every second (refer to attached video).
How do I solve this issue?
Thanks again, you have been such a great help! :D  

Daniel Servan

Hi,

I think your script is correct.
Anyway, use this script below.


var player = GetPlayer();

function date_time() {
var date = new Date();
var am_pm = "AM";
var hour = date.getHours();
if(hour>=12){
am_pm = "PM";
}
if(hour>12){
hour = hour - 12;
}
if(hour<10){
hour = "0"+hour;
}

var minute = date.getMinutes();
if (minute<10){
minute = "0"+minute;
}


player.SetVar("12Hour", hour+":"+minute+" "+am_pm);

}

setInterval(date_time,500);

Diarmaid Collins

Hey Daniel (or any Javascript experts out there).

That script just there is perfect and it works beautifully for my interface demo which will show the user's current time as they interact with the module.

However, I have a request to show time-based information within the display and I was wondering how could this be adjusted to have a variable display, say, 4 minutes earlier than the current time? Like in a text message timeline, for example.

4.36PM: Diarmaid wrote this.

%12Hour%: Diarmaid wrote this.

4.32PM: Diarmaid thought about writing this.

%12Hour_Minus4Mins%: Diarmaid thought about writing this.

I thought I could just use some mathematics but realised that it's a text string variable so I am stumped.

Diarmaid Collins

Thank you so much for the solution Jurgen. That is deeply, deeply appreciated.

If I wanted to use that new script AS WELL as the original, I would obviously have to rename the variable 12Hour to something new, like, 12HourMINUS4, so the only thing I would have to replace in your code would be on line 22, 

player.SetVar("12Hour", hour+":"+minute+" "+am_pm);

to

player.SetVar("12HourMINUS4", hour+":"+minute+" "+am_pm);

Would that be correct? :)

I know I am being a pain in the ass here, buuuuuuuut... both code scripts would have to be called on in the relevant slide? 

Is there any way the 12Hour value can be calculated, and THEN the new value, 12HourMINUSX, (automatically) derived from that?

Math Notermans

The functionname is the same too, so you either need to change that too...or change the function so it sets 2 variables. Assuming you using them on the same trigger. If you use them on 2 separate triggers due to the fact Storyline scope is local ( trigger A has no knowledge about trgger B ) you can do as you plan.