0

I'm using jQueryUI datepicker.

$('#myDiv').datepicker({
   onSelect: function(dateText){
      //my function
   });
});

dateText comes as yymmdd
Now is a Jan. I can select a current month's date (1-31) or other month's date (30, 31 Dec. and 1,2 Feb.)
My question is - can we check what month was selected (current, prev. or next) and create a if/else statement inside onSelect?
Something like

$('#myDiv').datepicker({
  onSelect: function(dateText){
    if( prevMonth ){
      //my function 1
    }else if ( nextMonth ){
      //my function 2
    }else{ //currentMonth
      //my function 3 
  });
});

Thanks!

sergey_c
  • 741
  • 9
  • 14

1 Answers1

0

I make a simple snippet to accomplish what you want. But be careful: you are only going to know if the selected month is before or after current month. I'm not comparing dates, just months. Comparing dates should be doing using comparing operators such as <,>,<=,=>,== (follow this link for more info: Compare two dates with JavaScript)

$(function () {
    $("#datepicker").datepicker({
        onSelect: function (dateText) {

            var d = new Date(dateText),
                date_month = d.getUTCMonth() + 1;

            var today = new Date(),
                today_month = today.getUTCMonth() + 1;

            if (date_month < today_month) {
                $('#result').html('Previous month selected');
            } else {
                if (date_month > today_month) {
                    $('#result').html('Next month selected');
                } else {
                    $('#result').html('Same month selected');
                }
            }
        }
    });
});

JSFiddle: http://jsfiddle.net/franverona/ZYB9M/

Community
  • 1
  • 1
Fran Verona
  • 5,438
  • 6
  • 46
  • 85