How convert input type=“date” in timestamp javascript-jquery

随声附和 提交于 2020-06-12 06:17:51

问题


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?


回答1:


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">



回答2:


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




回答3:


Here is a Solution ( Using pure js ) , I used the unary plus operator operator after converting the value into javascript date object.

function checkDateValue(){
  var dateConvertedToTimestamp = (+new Date(document.getElementById('date_value').value));
  
  document.getElementById('date_value_timestamp').innerHTML  = dateConvertedToTimestamp ;
}
<input type='date' id='date_value'>
<button onClick='checkDateValue()'> Submit </button>

<div>Timestamp:- <span id='date_value_timestamp'></span></div>



回答4:


You can use following code

<script type="text/javascript">
var d = new Date(parseInt($('[name="date_start"]').val()));
var n = d.getTime();
console.log(n);
</script>


来源:https://stackoverflow.com/questions/46929778/how-convert-input-type-date-in-timestamp-javascript-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!