1

I want to popup a message when user click on submit button between the date of 2012-01-22, 00:00 to 2012-01-25, 23:59. I write the code as below, but it won't works. Could someone suggest how to convert the date to integer so that I can check if today date is greater than x and less than x, then popup the message?

Thanks

function holiday_alert_msg() {      
    var today_date = new Date();

    if (today_date > 201201220000 && today_date < 201201252359) {
        alert("Today is holiday");
    }

}

$('#submit').bind('click', function() {
    holiday_alert_msg();
});
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130

2 Answers2

3

You can do this:

function holiday_alert_msg() {      
    var today_date = new Date();
    // The month parameter is 0 based
    if (today_date > new Date(2012,0,22) && today_date < new Date(2012,0,25,23,59)) {
        alert("Today is holiday");
    }

}

$('#submit').bind('click', holiday_alert_msg);
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Qorbani
  • 5,825
  • 2
  • 39
  • 47
  • 2
    I would suggest changing the second date comparison to `today_date < new Date(2012,0,26)` so you won't get a bug if you are a few milliseconds before the 26th – Ruan Mendes Jan 11 '12 at 04:59
0
var now = new Date();
console.log(now.getDate());     // 11   - day of month
console.log(now.getMonth());    // 0    - 0 indexed month (0 is January)
console.log(now.getFullYear()); // 2012 - 4 digit year

The Date prototype has some useful methods that can make it easier to compare these values. I leave how to use them as an exercise for you :)

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337