Skip to content
Advertisement

How convert input type=”date” in a timestamp?

I need to convert an <input type="date"> value 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 working… why not?

Advertisement

Answer

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">
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement