November 4, 2020
Using Javascript to display altered day/date in MM/DD/YYYY Format
Comments
(7)
November 4, 2020
Using Javascript to display altered day/date in MM/DD/YYYY Format
(7)

Hello everyone,

I’m trying to get Captivate 2019 to display the day and date but with an increment/decrement to the day/date.

I found a previous post where a user was able to successfully do this; however, it formats the date in DD/MM/YYYY format. I have never used JavaScript before and cannot figure out how to change the formatting to be MM/DD/YYYY.

Below is the extra code in standard.js followed by the simple script used to manipulate the date.  The result will be outputted as a variable so I can display it in a text caption.

Can anyone help with the formatting to have it shown in a US date format?

Thanks!

…………………………………………………………………………………………………………………………………..

function date_math(increment,return_var){

var now = new Date();

now.setDate(now.getDate() + parseFloat(increment)); var nowStr =  (now.getDate() < 10 ? “0” + now.getDate().toString() : now.getDate().toString()) + “/” + (now.getMonth()+1 < 10 ? “0” + (now.getMonth()+ 1).toString() : (now.getMonth()+1).toString()) + “/” + now.getFullYear().toString(); document.Captivate.cpEISetValue(return_var,nowStr);

}

date_math(-11,”DateMinus11Variable”);

date_math(-8,”DateMinus8Variable”);

date_math(-3,”DateMinus3Variable”);

date_math(-2,”DateMinus2Variable”);

date_math(-1,”DateMinus1Variable”);

date_math(0,”d0″);

date_math(28,”d_p28″);

date_math(15,”d_p15″);

7 Comments
2021-11-30 03:45:00
2021-11-30 03:45:00

wow thank you so much for actually posting the JS

Like
2020-11-05 18:00:13
2020-11-05 18:00:13

Thank you for sharing

Like
(4)
2020-11-05 15:35:33
2020-11-05 15:35:33

This may work for you.

function date_math(increment,return_var){
var now = new Date();

now.setDate(now.getDate() + parseFloat(increment));
var nowStr = (now.getMonth()+1 < 10 ? “0” + (now.getMonth()+1).toString() :
(now.getMonth()+1).toString()) + “/” + (now.getDate() < 10 ? “0” + (now.getDate() ).toString() : (now.getDate() ).toString()) + “/” + now.getFullYear().toString(); document.Captivate.cpEISetValue(return_var,nowStr);
}

date_math(-11,”DateMinus11Variable”);
date_math(-8,”DateMinus8Variable”);
date_math(-3,”DateMinus3Variable”);
date_math(-2,”DateMinus2Variable”);
date_math(-1,”DateMinus1Variable”);
date_math(0,”d0″);
date_math(28,”d_p28″);
date_math(15,”d_p15″);

Like
(4)
2020-11-05 13:44:20
2020-11-05 13:44:20

I will end my rant here.

Had to post pic because of editor fail

Attachment

Like
(5)
(1)
>
Greg Stager
's comment
2020-11-06 08:28:43
2020-11-06 08:28:43
>
Greg Stager
's comment

Completely agree, Greg. I would post an image but it takes more time of course.

Like
(3)
2020-11-05 13:26:04
2020-11-05 13:26:04

Forgive me for perhaps going in a slightly different direction here but perhaps it may help.

In this example I simply made my SmartShape with text as show in the picture provided.

I made three Captivate variables  mm,  dd,  and  yyyy  to display each thing separately.

I also made four buttons to increment and decrement the month and day.

There is a little JavaScript placed in the  onEnter  action of the slide – that is shown in the picture.

Then there is a little JavaScript on each button that just increments or decrements the value and lets it wrap around from 31 to 1 or 1 to 31 depending on the direction you are going. I did not add any logic to check the month and limit proper day choices.

//code to increment month
++mm;

if (mm==13) {
mm=1;
}

//code to decrement month
–mm;

if (mm==0) {
mm=12;
}

//code to increment day
++dd;

if (dd==32) {
dd=1;
}

//code to decrement day
–dd;

if (dd==0) {
dd=31;
}

Attachment

Like
(2)
(1)
>
Greg Stager
's comment
2020-11-05 13:36:50
2020-11-05 13:36:50
>
Greg Stager
's comment

In the code to decrement – it is actually two minus signs just like the
increment is two plus signs.
Looks like they got blended together.  
Gonna try new format option here.
–dd;

Like
Add Comment