live dynamic date and time with javascript

Aug 25, 2021

Just want to share here my code for a dynamic date and time -
So if someone want to have the date and the time in their slide updating live, this is the code:

function dateAndTime(){

let currentTime = new Date();

let monthArray = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");

let month = currentTime.getMonth();
let monthName = monthArray[month];
let day = currentTime.getDate();
let year = currentTime.getFullYear();
let hour = currentTime.getHours();
let minute = String(currentTime.getMinutes()).padStart(2, "0");
let second = String(currentTime.getSeconds()).padStart(2, "0");
let dateString= day + " " + monthName + " " + year + " " + hour + ":" + minute + ":" + second ;
let player = GetPlayer();
player.SetVar("SystemDate",dateString);

setTimeout( dateAndTime , 1000);

}

dateAndTime();
1 Reply
Brian Dennis

Nice bit of code Anna. A quick suggestion for your code. Consider switching to the following Date class initializer as that gets the learner's specific (timezone adjusted) clock and formats automagically. Then currentTime is effectively ready to (for date at least). And the setTimeout for one second is unnecessary. Here's my suggested change:

let currentTime = new Date().toLocaleString('default', { year: 'numeric', month: 'long', day: 'numeric' });

For future thread viewers, Anna's code requires you have created a project variable called SystemDate...