-1

I'm new to Javascript, and I'm trying to countdown the Saturdays until Halloween. Right now I have static HTML that says "16 Saturdays Left Until Halloween".

16 Saturdays Left Until Halloween!

I want it to refresh every saturday and change the number accordingly. If anyone could steer me in the right direction I would be appreciative.

Thanks.

tbml
  • 11
  • 1

2 Answers2

0

Use Javascript Date() objects to get the time interval between now and Halloween. Then you can extrapolate how many Saturdays there are in the time interval based on how many weeks there are between now and the time interval. This will always be up-to-date, so no need for a weekly update. Also you'll need to think about what you want to happen on a Saturday - is that counted in your count, or is that Saturday over?

Take a look at this answer for the mechanics: Get time difference between two dates in seconds

Community
  • 1
  • 1
sunny
  • 3,853
  • 5
  • 32
  • 62
0

Just count the weeks between today and the desired date and round them up. After that subtract 1 if the desired date is between monday and friday.

var today = new Date();
var dateHalloween = new Date(today.getFullYear() + '-10-31');

var weeksUntilHalloween = (dateHalloween - today) / 7 / 24 / 60 / 60 / 1000;
var saturdaysUntilHalloween = Math.ceil(weeksUntilHalloween);
if(dateHalloween.getDay() > 0 && dateHalloween.getDay() < 6) {
    saturdaysUntilHalloween--;
}
Sebastian Nette
  • 7,364
  • 2
  • 17
  • 17