Forum Discussion
Javascript variable / Current Date
Hello, Can someone give me the javascript to display the current date by using a variable? It is similar to the below javascript but I need it to display the date numerically like "06/20/2023"
var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }; var date = new Date().toLocaleTimeString('en-us', options); var player = GetPlayer(); player.SetVar("SystemDate",date);
16 Replies
- K-HCommunity Member
Is it possible to set the month as a text variable. I'm trying to create a certificate that caters to different regions where they may put the month ahead of the day. So I thought this might be a way to counter that and avoid confusion.
- DanielManitskyCommunity Member
Just a note- Make sure when creating your variable for todaysDate that it is a TEXT variable and not a number variable.
- KaitlynneBeck-0Community Member
This is super helpful and I'm looking at adding something similar. However, is it possible to add today's date but have it contingent on the time? I'm working to set the TRADE date for the stock market, and because the stock market closes at 4PM EST, I need the date to stay as the current date or change to the next business day if a trade happens after 4PM EST. Is this possible??
- MaureenMatsumotCommunity Member
Joe,
WOW! THANK YOU! Thank you for your quick reply and your solution. It worked perfectly.
I created a software simulation and wanted it to look "real." Now, it does, thanks to your help.
Thanks again!
Maureen
- Joe_DeyCommunity Member
- Joe_DeyCommunity Member
Hi Tina, try the attached to see if that helps you.
Edit: I have deleted the old template and replaced with the updated template below.
- MaureenMatsumotCommunity Member
Hi Joe,
Thank you for the storytemplate above. I used it and it works, however, I am unable to get a zero to appear in front of the two-digit month. Is there something I am missing? For example, today's date must appear as 05/19/2024 and it shows 5/19/2024.
Thanks for your help!
Maureen
- TinaGlynn-59324Community Member
This is really close to what I'm looking for, but I need the output to be a 2-digit month, 2-digit day, and 4-digit year with dashes instead of /. For example: 03-06-2024.
- XaxiaCastroCommunity Member
You are amazing! thanks!!!!
- ChrisRiley-8c05Community Member
Hi Greg,
You'll want to follow these steps to get the date to show up:
1) Create a Text Variable called todaysDate in Storyline to store the data.2) Add a text box to the slide with
%todaysDate%
as the value. The percent signs indicate that this placeholder should be replaced with the actual value stored in the todaysDate variable. You can put that placeholder anywhere in a text box, it doesn't have to be by itself. So you could have a text box that saysToday's date is %todaysDate%
and it would fill in the placeholder with the Variable value. Note that capitalization is important here - if the Variable is called todaysDate then a placeholder like%todaysdate%
won't work.3) Create a trigger with "Execute JavaScript" as the action, then copy and paste Steve's Javascript code into the Javascript editor in Storyline. (The last line in Steve's code will set a different Variable called "month", so you don't really need it).
let currentTime = new Date();
let month = currentTime.getMonth() + 1;
let day = currentTime.getDate();
let year = currentTime.getFullYear();
// Putting it together
let dateString = month + "/" + day + "/" + year;
//Pushing data to Storyline
let player = GetPlayer();
player.SetVar("todaysDate", dateString);- SamyStephensonCommunity Member
Thank you!!
- GregGilbert-9f3Community Member
HI Chris - This worked perfectly! Thank you so much! Greg
- GregGilbert-9f3Community Member
Hey. Never used JS before, but just want to show today's date on a certificate slide. Do I create a text box with %todaysdate%? Then, how do I use the JS to make it work? Thanks.