我想计算两个日期时间之间的差异,一个日期由用户提交,另一个日期为当前时间:
user submitted time - now = difference in unix
用户提交的时间格式为:
2014-03-26 10:52:00
谢谢你的帮助。
您只需执行此操作即可getTime()
返回毫秒数。
var ds = "2014-03-26 10:52:00";
var newDate = new Date(ds).getTime(); //convert string date to Date object
var currentDate = new Date().getTime();
var diff = currentDate-newDate;
console.log(diff);
有时在解析日期字符串时可能会与跨浏览器兼容,因此最好像这样解析它
var ds = "2014-03-26 10:52:00";
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
var currentDate = new Date().getTime();
var diff = currentDate - newDate;
console.log(diff); //timestamp difference
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句