0

I am using the following script to return a date for a web page, however when using this script twice on a page only one date is returned (even when using different IDs). Should I be using an array for the startDate?

<script>
let startDate = "1 January 2020";
let startDateObj = new Date(startDate);
let recurringDay = 14;
let today = new Date();
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let totalDays = Math.floor((today-startDateObj)/(1000 * 60 * 60 * 24));
let daysLeftToRecur = (recurringDay - totalDays%recurringDay) || recurringDay;
let nextDate = new Date(new Date().getTime()+(daysLeftToRecur * 1000 * 60 * 60 * 24));
document.getElementById("displaydate").innerHTML = days[nextDate.getDay()] + " " + nextDate.getDate() + " " + months[nextDate.getMonth()] + " " + nextDate.getFullYear();
</script>
cb17
  • 1
  • 2
  • Date will remain same for 24 hours. Time will differ. You are not printing the time. – Amit Kumar Singh Oct 05 '20 at 03:32
  • Correct. I have two different "startDates" and two different "ID"s and I need to include them all into this script. – cb17 Oct 05 '20 at 03:51
  • create a function. – Amit Kumar Singh Oct 05 '20 at 04:12
  • I am attempting the following but it is still only returning one value: let startDate = ["1 January 2020", "2 January 2020"]; let startDateObj = new Date(startDate[0, 1]); – cb17 Oct 05 '20 at 08:35
  • https://www.javascripttutorial.net/javascript-return-multiple-values/ – Amit Kumar Singh Oct 05 '20 at 08:51
  • Using the built–in parser for an unsupported format is a bad idea, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Adding days by adding time is also a bad idea, see [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date/9989458#9989458) – RobG Oct 05 '20 at 23:32
  • Hi @RobG, apologies I am only a beginner. I like the response you gave on the [Get next weekday](https://stackoverflow.com/questions/42612865/moment-js-get-next-weekday/42618434) thread. Could this work from a set start date eg. start date of 1 January 2020, and then displaying the next upcoming Thursday? – cb17 Oct 06 '20 at 10:08

0 Answers0