Copy link to clipboard
Copied
Does anyone know who to add in current dates into Captivate 9? I need to display the current date and time in the following formats on page load:
Fri, 1 Dec 17 1334 (using a 24 hour clock)
01Dec
1 Dec 17 1334
I can't seem to get the JavaScript for new Date to run correctly. Help would be appreciated. Thank you
Copy link to clipboard
Copied
Here's what I tried and it seemed to work.
1. Create two variables 'dt' for date, and 'tm' for time
2. Execute the following javascript 'on Enter'
var d = new Date();
dt = d.toDateString();
tm = d.toLocaleTimeString();
3. Create two caption boxes one with the dt variable, one with the tm variable.
Note that this will only load the time once. If you wanted to have a running clock, you would need to create a loop. Hope this helps.
Copy link to clipboard
Copied
Realized you shouldn't use a loop to update your clock. Use setInterval instead.
function getDate(){
d = new Date();
ti = d.toDateString();
dt = d.toLocaleTimeString();
}
setInterval(getDate, 1000);
Copy link to clipboard
Copied
Hello and thank you so very much for your help! This one worked but the second one didn't!
var d = new Date();
dt = d.toDateString();
tm = d.toLocaleTimeString();
I am creating a software simulation for a software that displays dates differently depending on what task you are completing. I need to show the dates separately as follows:
DayMonth (20Dec) no spaces (20Dec)
Day, Date Month YY and current time Wed, 20 Dec 17 0948
and then Date month year (2 digits for the date) and 2 hours in the future from the current date. 20 Dec 17 1148
Is there a way to modify the above to get the times to show up as above? Thank you so much!
Copy link to clipboard
Copied
I'm not 100% clear on all of the different configurations but here goes:
If you create a 'month', 'time', 'date', 'day', 'year' variable and plug in this code you will get the formats you want.
var d = new Date();
var daysArray = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
monthsArray = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
month = monthsArray[d.getMonth()]
time = d.toLocaleTimeString();
date = d.getDate()
day = daysArray[d.getDay()]
year = d.getFullYear()
If you create a single text caption, you can have multiple variables within it. For example '$$date$$$$month$$' would return 20Dec.
To add hours to the current time you would need to use this. Create a variable called twoHours
var future= new Date();
future.setHours(now.getHours() + 2)
twoHours = future.toLocalTimeString();
Hope that helps.
Copy link to clipboard
Copied
Thank you so very much! I have it working as I need it.
Copy link to clipboard
Copied
Thank you so very much. I have taken this and created what I need. Thank you again
Copy link to clipboard
Copied
Hello,
I have almost everything working except I discovered that the minutes display as a single digit until after the 10 minute mark. I will need this to show as 1401 rather than 1401. I would add a variance of the code below if this was to a website but don't know how to add it into Captivate. Thanks again for your help!
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
minutes = addZero(d.getMinutes());
Copy link to clipboard
Copied
I tested out the code and it works. It's 3:42 as I write this so I needed to subtract some time to make sure it worked. Just declare 'minutes' as a variable in Captivate (which makes it a global variable) and you are good to go.
var d = new Date();
var min = d.getMinutes()
min = min - 33
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
minutes = addZero(min);