Skip to main content
Known Participant
May 16, 2023
Question

Formatting Date Variables

  • May 16, 2023
  • 2 replies
  • 2249 views

Hi All,

I want to insert a date into a project (Captivate 2019).

 

cpInfoCurrentLocaleDateString is very, VERY close to what I want. BUT... on my system it displays the date as "Tuesday May 16, 2023" when what I would REALLY like is just:

"May 16, 2023" 

 

The issue appears to be local, in that this is the way I have my long date display set up in the Windows Control Panel.

If I try to roll my own month, day, year concatinating:

$$cpInfoCurrentMonth$$ $$cpInfoCurrentDate$$ , $$cpInfoCurrentYear$$
I get: "05 16, 2023"

 

What I would like is for the cpInfoCurrentMonth to display as "May" not "05", so the result would be: "May 16, 2023".  Can I get the "05" to display as "May"?

This has been asked before, but the links in the older responses no longer work.

Many thanks!

    This topic has been closed for replies.

    2 replies

    sabre123
    Participating Frequently
    May 16, 2023

    Another Javascript option would be to use the JS Date object:

     

    1. Create a Captivate variable. For this example it is named, myDate
    2. Next create a text caption, select the insert variable button, and choose the myDate var
    3. Then select Execute JavaScript from the On Enter drop down and add the following JS (including the Captivate cpAPIInterface function call to set the value of myDate to the value of the today var):

     

    const today = new Date().toLocaleDateString('en-us', { year:"numeric", month:"short", day:"numeric"}) ;
    
    window.cpAPIInterface.setVariableValue("myDate", today);

     

     

    There are certainly other ways to do it using JS, but that was one I'm familiar with...

    Lilybiri
    Legend
    May 17, 2023

    @sabre123 I have been using Date object as well, but was wondering what happens when the learner overrides the default setting for the System date? It is the reason why I also hesitate to use the system variable with LocalDateString. Please correct me, you are the JS expert.

    My second reason was that I often have to localise, and only the month needs to be changed. It is sufficient to replace the array by an array in the appropriate language. Of course that can also be done by replacing the 'en-us' in your solution. Probably less work.

    sabre123
    Participating Frequently
    May 17, 2023

     You bring up a valid point in terms of the JS Date obj pulling in the user's (possibly altered) system date. We are at the mercy of the user using the Date obj.

    You are correct regarding changing the lanuage in the Date object when you need to localise. The language displayed is pulled in via the locales parameter ('en-us' in this case). 

    Lilybiri
    Legend
    May 16, 2023

    You could use JavaScript to convert the month number from the system variable to its name using an array. Just checked this out, using v_month as user variable to store the month name. I am sure the JS experts have much better solutions, but you'll have to wait. Here is my experiment:

     

    const monthNames = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"];
    const monthNumber = window.cpAPIInterface.getVariableValue("cpInfoCurrentMonth");
    window.cpAPIInterface.setVariableValue("v_month", monthNames[monthNumber - 1]);

     

    You'll use the concatenation:

    $$v_month$$ $$cpInfoCurrentDate$$ , $$cpInfoCurrentYear$$

     

    As for the Localestring variable: it may display differently depending on the setup of the system date on the learner's system.

    Known Participant
    May 17, 2023

    Lilybiri -

    Your array JS works beautifully! Thank you so much! Just what I needed!

     

    The script seems resistant to selecting a different type face, but only I will be aware of that, and for the purpose the default sans serif typeface is more than adequate. 

     

    Your solution solves an irk and vexation with the standard Captivate options, so again, thank you for taking the time to develop the JS array and drafting your how-to. 

     

    This forum is a marvelous resource, thank you all!

     

    Don

    Lilybiri
    Legend
    May 18, 2023

    You're welcome. You may have read why I choose for this workflow in my answer to @sabre123 . I am a bit confused because what you stated about the font? Font normally is defined in Captivate since you insert variable values which take on the font defined for the text container.