最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert time to timestamp in js - Stack Overflow

programmeradmin4浏览0评论

hi i have a time input tag and when i click a button i need to get time from that input tag and convert it to timestamp. Here is my code,

HTML code:

<input type="time" class="timein"/>
<input type="button"class="convert_timestamp"/>

JS code

$('body').on("click",".convert_timestamp",function(){
var tm = $(".time").val();
var tmp = Date.now(tm);
});

We usually get current timestamp using Date.now() . In this case how to get a timestamp by sending a custom time. Can someone please help me with this? . Thanks in advance.

hi i have a time input tag and when i click a button i need to get time from that input tag and convert it to timestamp. Here is my code,

HTML code:

<input type="time" class="timein"/>
<input type="button"class="convert_timestamp"/>

JS code

$('body').on("click",".convert_timestamp",function(){
var tm = $(".time").val();
var tmp = Date.now(tm);
});

We usually get current timestamp using Date.now() . In this case how to get a timestamp by sending a custom time. Can someone please help me with this? . Thanks in advance.

Share Improve this question asked Jul 3, 2017 at 8:56 Sundar NivashSundar Nivash 3066 silver badges26 bronze badges 1
  • Is the user free to insert time in every format? In this case you shoud differentiate each case, maybe using regex... – decadenza Commented Jul 3, 2017 at 8:58
Add a ment  | 

5 Answers 5

Reset to default 4

var tmp = new Date(tm).getTime() // 1499072255507

For instance :

new Date("2017/07/03") // Mon Jul 03 2017 00:00:00 GMT+0200 (W. Europe Daylight Time)

new Date("2017/07/03").getTime() // 1499032800000

You can create a new Date with new :

var d = new Date(tm);

You can also get the timestamp using the plus operator which triggers the valueOf of the targeted object :

var timestamp = +d;

Assuming the string is in the correct format the following should work

$('body').on("click",".convert_timestamp",function(){
    var tm = $(".time").val();
    var tmp = new Date(tm).getTime();
    });

try this,

$('.convert_timestamp').on("click",function(){
var tm = $(".timein").val();
var tmp = Date(tm);
var only_time = Date.now(tm);
console.log("time with date" ,tmp);    
console.log("only time", only_time);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input type="time" class="timein"/>
<input type="button" value="gen Timestamp" class="convert_timestamp"/>

$('body').on("click",".convert_timestamp",function(){
var tm = $(".time").val();
var tmp = new Date(tm).getTime();
// now tmp contain 2165465498764 
});
发布评论

评论列表(0)

  1. 暂无评论