I need to convert an <input type="date">
value in a timestamp. This is my HTML code:
JavaScript
x
2
1
<input type="date" name="date_end" id="date_end">
2
This field has a value that I have put like 25/10/2017
My jQuery code is:
JavaScript
1
9
1
var dataEnd = $('[name="date_end"]').val();
2
if (!dataEnd) {
3
return false;
4
} else {
5
var timestamp_end=$('[name="date_start"]').val().getTime();
6
console.log("TIMESTAMP END "+timestamp_end);
7
..
8
}
9
But this is not working… why not?
Advertisement
Answer
make a new Date()
passing the value of your input as parameter, then call getTime()
. here an example:
JavaScript
1
4
1
$('[name="date_end"]').on('change',function() {
2
var dataEnd = $(this).val();
3
console.log((new Date(dataEnd)).getTime());
4
})
JavaScript
1
2
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<input type="date" name="date_end" id="date_end">