我想检查时间12.55(这是那里的格式,我无权编辑格式)是否大于2个日期之间的总时间,如2015-10-12 10:00“和2015- 10-12 12:00。我已经完成了发现差异的第一部分但不知道做第二部分,发现时间大于时差。希望有人可以帮助我!
var getDayTimeDiff = function(start, end){
var date1 = new Date(start);
var date2 = new Date(end);
var diff = date2.getTime() - date1.getTime();
var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;
return diff;
}
var timeDiff = getDayTimeDiff("2015-10-12 10:00","2015-10-12 12:00");
console.log(timeDiff); // 7200000
var time2Calc = "12.55";
答案 0 :(得分:1)
在JavaScript中,使用Moment.js可以轻松处理日期和时间操作 喜欢
var duration = moment.duration(end.diff(startTime));
var hours = duration.asMilliseconds();
答案 1 :(得分:0)
您可以将时间转换为毫秒,然后比较差异,例如
var getDayTimeDiff = function(start, end) {
var date1 = new Date(start);
var date2 = new Date(end);
return date2.getTime() - date1.getTime();
}
var toMillis = function(time) {
var parts = time.split('.');
return (parts[0] * 60 + +parts[1]) * 60 * 1000;
}
function test(t1, t2) {
var timeDiff = getDayTimeDiff(t1, t2);
var time2Calc = "12.55";
var base = toMillis(time2Calc);
var result = (Math.abs(timeDiff) > base);
snippet.log(t1 + ' : ' + t2 + ' -> ' + result);
}
test("2015-10-12 10:00", "2015-10-12 23:00");
test("2015-10-12 10:00", "2015-10-12 12:00");
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
答案 2 :(得分:0)
如果按'时间12.55'表示12小时55分钟,只需将其转换为毫秒并与timeDiff进行比较。
<强> E:强>
function toMilli(time) {
var a = time.split(".");
return (a[0]*60 + a[1])*60*1000;
}