Reputation: 33
I would to run .js script only if the date is between December 15 and January 8 (of the next year) so the script will run for 25 days.
My code is:
<script type="text/javascript">
var now = new Date();
var day = now.getDate(); // 0-31 Days
var month = now.getMonth()+1; // 0-11 Months
var startDate = new Date();
startDate.setDate(15);
startDate.setMonth(11);
var endDate = new Date();
endDate.setDate(8);
endDate.setMonth(0);
if (startDate >= now && endDate <= now) {
snowFall.snow(document.body);
}
the .js file is located <script src="js/snowfall.js"></script>
What's wrong with the code?
My html/javascript code level is very bad, I'm a newbie
Thanks for the help!!!
Upvotes: 0
Views: 382
Reputation: 138277
Just change your condition from:
if (startDate >= now && endDate <= now)
to
if (startDate <= now || endDate >= now)
That works as the the dates are always of the same year, so startDate <= now
works between 15.11.2017 - 31.12.2017
and endDate >= now
will work between 1.1.2018 - 31.1.2018
Upvotes: 1