I'm using an HTML input field of a date:
<input id="datepicker" type="date" value="11/12/2013" />
How can I get using JQuery the different values for the year, month and day? each one need to be save as a different var
Thanks,
I'm using an HTML input field of a date:
<input id="datepicker" type="date" value="11/12/2013" />
How can I get using JQuery the different values for the year, month and day? each one need to be save as a different var
Thanks,
You can use split by /
to get them in array individually:
var date=$('#datepicker').val().split('/');
var day=date[0];
var month=date[1];
var year=date[2];
EDIT: For this to work you need to change type="text" OR you look at this post: set date in input type date
var d = $('#datepicker').val().split('/');
var year = d[2];
var month = d[1];
var day = d[0];
Supposing you are using the DD/MM/YYYY format.
try like this:
Html:
<input id="datepicker" type="date" value="2013-11-11" />
JavaScript:
var date = $('#datepicker').val().split('-');
var year = date[2];
var month = date[1];
var day = date[0];
Note: type date take value in yyyy-mm-dd format
With Javascript es6 you can do a one-liner
let [day,month,year] = $('#datepicker').val().split('-');