How convert input type="date" in timestamp javascript-jquery

Polly picture Polly · Oct 25, 2017 · Viewed 12.1k times · Source

I need to convert a in a timestamp, this is my html code:

 <input type="date" name="date_end" id="date_end">

This field has a value that I have put like 25/10/2017 My jquery code is:

var dataEnd = $('[name="date_end"]').val();
        if (!dataEnd) {
            return false;
        } else {
            var timestamp_end=$('[name="date_start"]').val().getTime();
            console.log("TIMESTAMP END "+timestamp_end);
.....
}

But this is not work, anyone can help me?

Answer

Roberto Bisello picture Roberto Bisello · Oct 25, 2017

make a new Date() passing the value of your input as parameter, then call getTime(). here an example:

$('[name="date_end"]').on('change',function() {
  var dataEnd = $(this).val();
  console.log((new Date(dataEnd)).getTime());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" name="date_end" id="date_end">