How to extract values from HTML <input type="date"> using jQuery
Date value returned by input type="date"
is format yyyy-mm-dd
. Could use .split()
with argument "-"
to retrieve array containing [yyyy, mm, dd]
Note, alert()
expects string as parameter ; does not print values same as console.log()
with comma ,
operator
var day, month, year;
$('#submit').on('click', function() {
var date = $('#date-input').val().split("-");
console.log(date, $('#date-input').val())
day = date[2];
month = date[1];
year = date[0];
alert(day + month + year);
});
jsfiddle https://jsfiddle.net/dkxy46ha/2/
date = new Date($('#date-input').val())
date.getDate()
...
Firstly you need to create a Date
object from input
element value. And then you will be able to get day, month and year from this object.
$('#submit').on('click', function(){
var date = new Date($('#date-input').val());
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
alert([day, month, year].join('/'));
});
Working example: https://jsfiddle.net/8poLtqvp/