1

I can't find an answer to my question, I would like to enable a javascript script on a date and disable it on another date, for example, I have a code to transform my website with snow on the page, and I would like to enable it on the 1st December and disable it the 31 December of each year automatically, is it possible ?

And is it possible in a javascript on an HTML page too?

Julian E.
  • 4,687
  • 6
  • 32
  • 49
Playz
  • 23
  • 1
  • 4
  • If your page is dynamic you can choose to insert script tag on server side based on the date. You can also have JavaScript on your page which dynamically requests the snow script based on the current day. There are several ways to do this. – hazardous Dec 20 '15 at 13:04
  • 1
    Yes it's possible in Javascript, but you have no control over the users clock. – Xotic750 Dec 20 '15 at 13:05

3 Answers3

1

Here is a quick example of a script that will load another js script only if date is in december.

After this, just make sure that the other script will make it snow :)

var date = new Date();
// This is a quick check for december month
// Can be replaced with a check for month/day
if (date.getMonth() === 11) {
  var snowJs = document.createElement("script");
  snowJs.type = "text/javascript";
  snowJs.src = "path-to-snow-js";
  document.body.appendChild(snowJs);
}
0

If its a part of existing file and cannot be separated, then just add a filter for valid dates. Following is the example for it:

If not, you can dynamically load this file on valid days and call function.

(function(){
  function notify(date){
    console.log(date);
  }
  
  function getValidDates(){
    // You can have a API call that can get dates from server
    var validDates = {
      startDate: new Date(2015,11,1),
      endDate: new Date(2015,11,31),
      today: new Date()
    }
    return validDates;
  }
  
  function validate(dates){    
    if(+dates.startDate <= +dates.today && +dates.endDate> +dates.today){
      notify(dates.today);
    }
    else{
      console.log(dates);
    }
  }
  
  function main(){
    validate(getValidDates());
  }
  
  main();
})()
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

i know it's not the best. but i think it'll be useful for you. 1- you can use Date(); function in java script. but we have a problem about it. it get time from user system and it could be wrong.

you can read about it in this page

var today = new Date();
var todayMonth = today.getMonth()+1;

2- you can use a server side language to get time from it and you will have your server time for all visitor. like in php date("Y-m-d") . and you can learn here OR here

var todayMonth = <?php echo date("m") ?>

3- you can get it from third party application. but that is so Amateur and its not really true.

at the end you can get current month and year from this two functions. and compare it with two static value

if(todayMonth == 11 ) {
 // your code
} 
Community
  • 1
  • 1
a828h
  • 146
  • 9
  • Thank you very much, i think it's not important if i take the time from the user system because it's normally the good date, i will test ! thank you ! – Playz Dec 20 '15 at 13:34