Forum Discussion
Update a variable with the current date and time
Is it possible to insert the date and time into a variable to be displayed on screen.
What I want to achieve:
To time an activity that takes place over a number of slides.
Happy to use Javascript - any help is appreciated.
40 Replies
- sohailarifCommunity Member
In JavaScript you can create date & time objects using the Date constructor. Then object can use its own methods for different computations afterward.
There are number of ways in which you can create the date object via Date constructor.
Example
var today_date_time = new Date(); // Get today's date and time
var old_date = new Date(2010, 4, 1); // Date Literal
var old_date_time = new Date(2010, 0, 1, 18, 10, 30); // Date literal + time
var elapsed = today_date_time - old_date ; // Get the interval after subtraction in milliseconds
// Methodsold_date.getFullYear() // get year
old_date.getMonth() // get month number (starting from zero)
old_date.getDate() // get day
old_date.getDay() // get day of week (0-6) Starting from Sunday: 0
old_date.getHours() // get local time
old_date.getUTCHours() // get time in UTCSee working example at:
Thanks Sohail for sharing that here and providing some code for folks to use.
- JillFerrierCommunity Member
Huge help, thank you!
Glad to see that this thread was able to assist you as well Jill :)
- MartinVolckartCommunity Member
Hello David,
could you tell me how the time code works for Germany? We Count 24 Hours a day without AM or PM.
Thank you very much.
- MichaelHinzeCommunity Member
I think this simplified script here should do the trick;
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = day + "/" + month + "/" +year
player.SetVar("DateValue", newName);
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var CurTime = hours + ":" + minutes;
player.SetVar("TimeValue",CurTime);- MartinVolckartCommunity Member
Hello Michael,
perfect! Thanks a lot for your invaluable help.
Best regards
Martin
Von: E-Learning Heroes [mailto:heroes@articulate.com]
Gesendet: Donnerstag, 8. November 2018 17:24
An: Martin Volckart
Betreff: Reply to Update a variable with the current date and time
Hi Martin,
I'm not sure if David is still subscribed here as I saw he also didn't answer Jacqui's similar question. I don't know much (or anything really!) about Javascript code, so I hope someone here in the community can pop in to assist. You could also try using the "contact me" button on a ELH users' profile to reach out to them directly for advice! Best of luck with your project!
Glad that worked, Martin!
Just an FYI - It looks like your email signature came through when you replied via email. You can remove that if needed by clicking ‘Edit’ beneath your response. Here’s a quick Peek video if you need help.
- MartinVolckartCommunity Member
Hi Michael,
could you post the code. I use SL3 and cannot open the project file.
Thanks again
Martin
Hi Martin,
No worries, I opened the file shared above and switched from the Modern Player to Classic Player and you can open this file in SL3 now :)
- IonutMarin-9e52Community Member
hi everyone,
any chance to get hep on how to delay quiz attempts with x hours?
more exactly I am looking to add x hours between the current date/time (of the 1st attempt) and the next attempt.
thank you,
Ionut
Related Content
- 10 months ago
- 10 months ago
- 10 months ago