我有2017-07-25 09:30:49
这样的日期,当我减去2017-07-25 10:30:00
和2017-07-25 09:30:00
时,我需要1 Hours
之类的结果。
我找不到正确的搜索键来搜索我需要的内容。 任何人都知道我应该在谷歌上搜索什么?或者有人知道某些功能?
PS。 Mysql或Javascript
答案 0 :(得分:1)
在javascript中尝试使用日期对象
喜欢这个
var d1 = new Date("2017-07-25 10:30:00");
var d2 = new Date("2017-07-25 09:30:49")
var diff = Math.abs(d1-d2); // difference in milliseconds
然后将毫秒转换为小时
var hours = parseInt((diff/(1000*60*60))%24);
答案 1 :(得分:1)
你可以通过它 Get the time difference between two datetimes
但查询不清楚您是否只想要小时差异,或者您希望差异转换为小时格式
如果2017-07-25 09:30:49和2017-07-26 10:30:00将会给出什么? 25小时或1小时?
答案 2 :(得分:1)
这里有一个如何操作的代码示例
var date1 = new Date("2017-07-25 09:30:49");
var date2 = new Date("2017-07-25 10:30:00");
var datesum = new Date(date1 - date2);
var hours = datesum.getHours();
var minutes = datesum.getMinutes();
var seconds = datesum.getSeconds();
console.log(hours + " hour, " + minutes + " minutes, " + seconds + " seconds" )

答案 3 :(得分:0)
var dateString = "2017-07-25 09:30:49";
var dateString2= "2017-07-25 11:30:00";
var reggie = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var dateArray = reggie.exec(dateString);
var dateArray2= reggie.exec(dateString2);
var dateObject1= new Date(
(+dateArray[1]),
(+dateArray[2])-1, // Careful, month starts at 0!
(+dateArray[3]),
(+dateArray[4]),
(+dateArray[5]),
(+dateArray[6])
);
var dateObject2= new Date(
(+dateArray2[1]),
(+dateArray2[2])-1, // Careful, month starts at 0!
(+dateArray2[3]),
(+dateArray2[4]),
(+dateArray2[5]),
(+dateArray2[6])
);
var diff = Math.abs(dateObject2-dateObject1); // difference in milliseconds
var hours = parseInt((diff/(1000*60*60))%24);
答案 4 :(得分:0)
尝试使用以下dateFormatter函数:
var d1 = new Date("2017-07-25 10:30:00");
var d2 = new Date("2017-07-25 09:30:00")
var diff = Math.abs(d1-d2);
var d = dateFormatter(diff);
console.log(d);
function dateFormatter(t){
var cd = 24 * 60 * 60 * 1000;
var ch = 60 * 60 * 1000;
var cm = 60*1000;
var d = Math.floor(t / cd);
var h = '0' + Math.floor( (t - d * cd) / ch);
var m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
var s = '0' + Math.round((t - (d * cd) - (h * ch) - (m * cm))/1000);
return d + " days, " + h.substr(-2) + " hours, " + m.substr(-2) + " minutes, " +s.substr(-2)+ " seconds";
}