0

I want to get date day and month into separate variable and then place that variable into a div tag. Tell me where I need to change the code for that purpose.

$('.from_date').datepicker({
    dateFormat: "yy-mm-dd", 
    onSelect: function(){
      var selected = $(this).val();
      alert(selected);
    }
  });
});
αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
  • What you are getting as 'selected'? – αƞjiβ Apr 26 '16 at 16:54
  • 1
    Possible duplicate of [How to get date, month, year in jquery ui datepicker?](http://stackoverflow.com/questions/16186386/how-to-get-date-month-year-in-jquery-ui-datepicker) – αƞjiβ Apr 26 '16 at 16:55

3 Answers3

1

Parse your selected variable:

var
  selected = '2016-03-26';
  selectedToArray = selected.split('-'),
  year = selectedToArray[0], // 2016
  month = selectedToArray[1], // 03
  day = selectedToArray[2]; // 26

The split() method splits a String object into an array of strings by separating the string into substrings. You can read more about this function here.

Update:

$('.from_date').datepicker(
  { 
    dateFormat: "yy-mm-dd", 
    onSelect: function() { 

      var 
        selected = $(this).val(),
        selectedToArray = selected.split('-'),
        year = selectedToArray[0],
        month = selectedToArray[1],
        day = selectedToArray[2];

      $('#some_div_for_year').text(year);
      $('#some_div_for_month').text(month);

    } 
  }
); 
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
  • $('.from_date').datepicker({ dateFormat: "yy-mm-dd", onSelect: function(){ var selected = $(this).val(); alert(selected); } }); where to place your code? – Enam Ajmal Apr 26 '16 at 16:59
0

Try the javascript method "Split"

var rawValue = '90-03-31';
var year = rawValue.split('-')[0]; // returns "90"
var month = rawValue.split('-')[1]; // returns "03"
var day = rawValue.split('-')[2]; // returns "31"
0

parse date

$('.from_date').datepicker({ dateFormat: "yy-mm-dd", onSelect: function(){ 

       var selected = $(this).val(), 
       selectedArray = selected.split('-'),
       year = selectedArray [0],
       month = selectedArray [1],
       day = selectedArray [2];
} 
}); });
codeGig
  • 1,024
  • 1
  • 8
  • 11