Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 250
Polly Avatar asked Oct 25 '17 10:10

Polly


2 Answers

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">
like image 62
Roberto Bisello Avatar answered Sep 26 '22 03:09

Roberto Bisello


do this

var dateEnd = $('#date_end').val()
var var timestamp_end = Date.parse(date_end)

or in a single line

var timestamp_end = Date.parse($('#date_end').val())

it works and it's clean

like image 42
Jalasem Avatar answered Sep 27 '22 03:09

Jalasem