Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript for specific dates and times

Community Beginner ,
Dec 01, 2017 Dec 01, 2017

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

734
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2017 Dec 05, 2017

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 05, 2017 Dec 05, 2017

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 20, 2017 Dec 20, 2017

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 20, 2017 Dec 20, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 20, 2017 Dec 20, 2017

Thank you so very much! I have it working as I need it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 20, 2017 Dec 20, 2017

Thank you so very much. I have taken this and created what I need. Thank you again

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 21, 2017 Dec 21, 2017

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());

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 21, 2017 Dec 21, 2017
LATEST

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources