JavaScript - 两个时间字符串之间的差异
JavaScript - difference between two time strings
我需要比较两个不同的日期时间字符串(格式:YYYY-MM-DD'T'HH:mm)。
这是日期时间字符串:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
我已经把日期切掉了,所以我只需要时间(格式:HH:mm)。
var now = a.slice(11, 16);
var then = b.slice(11, 16);
// now = 10:45
// then = 12:15
有什么方法可以区分这两个时间?
结果应如下所示:
1 hour 30 minutes
此外,如果日期不同,是否有任何简单的解决方案来获得日期差异?
使用javascript日期:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
var milliseconds = ((new Date(a)) - (new Date(b)));
var minutes = milliseconds / (60000);
这应该让你开始:
d1 = new Date(Date.parse("2017-05-02T10:45"));
d2 = new Date(Date.parse("2017-05-02T12:15"));
var getDuration = function(d1, d2) {
d3 = new Date(d2 - d1);
d0 = new Date(0);
return {
getHours: function(){
return d3.getHours() - d0.getHours();
},
getMinutes: function(){
return d3.getMinutes() - d0.getMinutes();
},
getMilliseconds: function() {
return d3.getMilliseconds() - d0.getMilliseconds();
},
toString: function(){
return this.getHours() + ":" +
this.getMinutes() + ":" +
this.getMilliseconds();
},
};
}
diff = getDuration(d1, d2);
console.log(diff.toString());
或使用 momentjs 因为,1. 它经过了很好的测试并跟踪了错误。 2. 从头开始编码是一种有趣的学习体验,但如果你在公司环境中,从头开始编码会浪费时间(从而浪费金钱)。
我有一个库可以让这个变得简单:
wiki:https://github.com/jiangbai333/Common-tools/wiki/format
代码:https://github.com/jiangbai333/Common-tools/blob/dev/string/string.js
在你的文件中包含string.js,那么:
var temp = "short-stamp".format(+new Date("2017-05-02T12:15")) - "short-stamp".format(+new Date("2017-05-02T10:45"));
console.log(parseInt(temp / 3600), "hour", parseInt(temp % 3600 / 60), "minutes")
我需要比较两个不同的日期时间字符串(格式:YYYY-MM-DD'T'HH:mm)。
这是日期时间字符串:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
我已经把日期切掉了,所以我只需要时间(格式:HH:mm)。
var now = a.slice(11, 16);
var then = b.slice(11, 16);
// now = 10:45
// then = 12:15
有什么方法可以区分这两个时间?
结果应如下所示:
1 hour 30 minutes
此外,如果日期不同,是否有任何简单的解决方案来获得日期差异?
使用javascript日期:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
var milliseconds = ((new Date(a)) - (new Date(b)));
var minutes = milliseconds / (60000);
这应该让你开始:
d1 = new Date(Date.parse("2017-05-02T10:45"));
d2 = new Date(Date.parse("2017-05-02T12:15"));
var getDuration = function(d1, d2) {
d3 = new Date(d2 - d1);
d0 = new Date(0);
return {
getHours: function(){
return d3.getHours() - d0.getHours();
},
getMinutes: function(){
return d3.getMinutes() - d0.getMinutes();
},
getMilliseconds: function() {
return d3.getMilliseconds() - d0.getMilliseconds();
},
toString: function(){
return this.getHours() + ":" +
this.getMinutes() + ":" +
this.getMilliseconds();
},
};
}
diff = getDuration(d1, d2);
console.log(diff.toString());
或使用 momentjs 因为,1. 它经过了很好的测试并跟踪了错误。 2. 从头开始编码是一种有趣的学习体验,但如果你在公司环境中,从头开始编码会浪费时间(从而浪费金钱)。
我有一个库可以让这个变得简单:
wiki:https://github.com/jiangbai333/Common-tools/wiki/format 代码:https://github.com/jiangbai333/Common-tools/blob/dev/string/string.js
在你的文件中包含string.js,那么:
var temp = "short-stamp".format(+new Date("2017-05-02T12:15")) - "short-stamp".format(+new Date("2017-05-02T10:45"));
console.log(parseInt(temp / 3600), "hour", parseInt(temp % 3600 / 60), "minutes")