Forum Discussion
Javascript Date plus one, plus two etc...
I wonder if any Javascript whizzes can help? I have a very very basic grasp, enough to be able to copy and make some basic edits to get some minor outcomes. I have a slide needing me to calculate and display the date, multiple days before and after today's date.
At the moment, I have it working by having multiple individual Execute javascript triggers each with a version of the below for any of the number of days I want (for this slide I want back 4, 5, 6 and 10; and forward 1, 2 and 3):
//1DaysAhead
var currentDate = new Date();
var oneDaysAhead = new Date(currentDate);
oneDaysAhead.setDate(currentDate.getDate() + 1);
var oneDaysAheadmonth = oneDaysAhead.getMonth() + 1;
var oneDaysAheadDay = oneDaysAhead.getDate();
var oneDaysAheadYear = oneDaysAhead.getFullYear();
const oneDaysAheadMonth = oneDaysAhead.toLocaleString('default', { month: 'short' });
var oneDaysAheadDateString = oneDaysAheadDay + " " + oneDaysAheadMonth + " " + oneDaysAheadYear;
let player = GetPlayer();
player.SetVar("oneDaysAhead", oneDaysAheadDateString);
When I try combining the multiple working individual javascript triggers versions into one trigger (easier to edit down the line rather than opening multiple triggers), it doesn't work and my Javascript knowledge isn't sufficient to detect the issue. e.g. the below does not work:
var currentDate = new Date();
//1DaysAhead
var oneDaysAhead = new Date(currentDate);
oneDaysAhead.setDate(currentDate.getDate() + 1);
var oneDaysAheadmonth = oneDaysAhead.getMonth() + 1;
var oneDaysAheadDay = oneDaysAhead.getDate();
var oneDaysAheadYear = oneDaysAhead.getFullYear();
const oneDaysAheadMonth = oneDaysAhead.toLocaleString('default', { month: 'short' });
var oneDaysAheadDateString = oneDaysAheadDay + " " + oneDaysAheadMonth + " " + oneDaysAheadYear;
let player = GetPlayer();
player.SetVar("oneDaysAhead", oneDaysAheadDateString);
//2DaysAhead
var twoDaysAhead = new Date(currentDate);
twoDaysAhead.setDate(currentDate.getDate() + 2);
var twoDaysAheadmonth = twoDaysAhead.getMonth() + 1;
var twoDaysAheadDay = twoDaysAhead.getDate();
var twoDaysAheadYear = twoDaysAhead.getFullYear();
const twoDaysAheadMonth = twoDaysAhead.toLocaleString('default', { month: 'short' });
var twoDaysAheadDateString = twoDaysAheadDay + " " + twoDaysAheadMonth + " " + twoDaysAheadYear;
let player = GetPlayer();
player.SetVar("twoDaysAhead", twoDaysAheadDateString);
Is anyone able to point me in the right direction? My guesses are the re-use of currentDate and/or the player.SetVar lines.
Try this:
var currentDate = new Date(); var player = GetPlayer(); //1DaysAhead var oneDaysAhead = new Date(currentDate); oneDaysAhead.setDate(currentDate.getDate() + 1); var oneDaysAheadDateString = oneDaysAhead.getDate() + " " + oneDaysAhead.toLocaleString('default', { month: 'short' }) + " " + oneDaysAhead.getFullYear(); player.SetVar("oneDaysAhead", oneDaysAheadDateString); //2DaysAhead var twoDaysAhead = new Date(currentDate); twoDaysAhead.setDate(currentDate.getDate() + 2); var twoDaysAheadDateString = twoDaysAhead.getDate() + " " + twoDaysAhead.toLocaleString('default', { month: 'short' }) + " " + twoDaysAhead.getFullYear(); player.SetVar("twoDaysAhead", twoDaysAheadDateString); // add your other offsets the same way…The issue is that in JavaScript you cannot redeclare the same variable name (player) twice with let inside the same block. Redeclaring let player again causes the script to stop. Also, you don’t need to recalculate currentDate inside each block.
3 Replies
- PeterGrennanCommunity Member
Amazing, this worked absolutely perfectly. Thank you so much for your help!
- PeterGrennanCommunity Member
Looks great - it's well past the end of my day here so I'll attack this fresh in the morning and mark the solution assuming it all works - thanks for such a speedy reply!
- NedimCommunity Member
Try this:
var currentDate = new Date(); var player = GetPlayer(); //1DaysAhead var oneDaysAhead = new Date(currentDate); oneDaysAhead.setDate(currentDate.getDate() + 1); var oneDaysAheadDateString = oneDaysAhead.getDate() + " " + oneDaysAhead.toLocaleString('default', { month: 'short' }) + " " + oneDaysAhead.getFullYear(); player.SetVar("oneDaysAhead", oneDaysAheadDateString); //2DaysAhead var twoDaysAhead = new Date(currentDate); twoDaysAhead.setDate(currentDate.getDate() + 2); var twoDaysAheadDateString = twoDaysAhead.getDate() + " " + twoDaysAhead.toLocaleString('default', { month: 'short' }) + " " + twoDaysAhead.getFullYear(); player.SetVar("twoDaysAhead", twoDaysAheadDateString); // add your other offsets the same way…The issue is that in JavaScript you cannot redeclare the same variable name (player) twice with let inside the same block. Redeclaring let player again causes the script to stop. Also, you don’t need to recalculate currentDate inside each block.