如何获得使用时刻的不同时差?

时间:2018-07-30 13:50:16

标签: javascript datetime momentjs

我需要使用时刻获取hh:mm:ss格式的两个日期之间的持续时间。但我无法得到确切的区别。这是我的例子。

let start = moment("2018-07-27T14:56:33.763Z");
let end = moment("2018-07-28T14:56:33.763Z");
let diff = end.diff(start);

let f = moment.utc(diff).format("HH:mm:ss");
alert(f);

对于上述日期,我需要将输出作为24:00:00,因为这是一天的差异。但是我得到00:00:00作为回应。

如果我要更改小时数,则应基于此显示。我怎样才能做到这一点?有人可以帮我解决这个问题。

7 个答案:

答案 0 :(得分:1)

关于Moments GitHub Page的讨论相当漫长,但这可能是您所追求的:

let start = moment("2018-07-27T14:56:33.763Z");
let end = moment("2018-07-28T14:56:33.763Z");
let duration = moment.duration(end.diff(start));
let f = Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss")

alert(f);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

顺便说一下,有人为此特定问题创建了一个plugin,该消息也发布在Moments Website上。

答案 1 :(得分:0)

尝试一下:

let f = moment.utc(diff).format('D[days] H[ hours]');

Example

如果您只想将时间转换为HH:mm:ss,则可以手动将其转换为毫秒,然后将其解析为小时。

答案 2 :(得分:0)

请尝试以下方法来完成任务。

let start = moment("2018-07-27T14:56:33.763Z");
let end = moment("2018-07-28T14:56:33.763Z");
let diff = end.diff(start);

var d = moment.duration(diff, 'milliseconds');
var hours = Math.floor(d.asHours());
var mins = Math.floor(d.asMinutes()) - hours * 60;
var secs = Math.floor(d.asSeconds()) - mins * 60 - hours * 60*60;;
console.log(  hours + ":" + mins + ":" + secs);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

答案 3 :(得分:0)

它说明

enter code here
H, HH       24 hour time
h, or hh    12 hour time (use in conjunction with a or A)

因此,将您的时间表示为HH将为您提供24h格式,而hh将为您提供12h格式。

答案 4 :(得分:0)

由于时差是一天,所以小时,分钟,秒返回零,因此以这种方式更改条件

let f = moment.utc(diff).format("HH:mm:ss");
if(f=="00:00:00"){
f=moment.utc(diff).format("D[days] H[hours]");
}

答案 5 :(得分:0)

您正在做的是尝试格式化两个日期的差异,这将导致日期格式的字符串,您需要做的是获取两个日期的小时数差异,为此,您需要使用duration方法:

let start = moment("2018-07-27T14:56:33.763Z");
let end = moment("2018-07-28T13:56:33.763Z");
let diff = end.diff(start);

let diffData = moment.duration(diff);
alert((diffData.days() * 24) + diffData.hours());

答案 6 :(得分:0)

这将为您提供帮助。

.toTypedArray()
var units = ["year", "month", "day", "hour", "minute", "second", "millisecond"];
var unitmapping = {month:12,day:30,hour:24,minute:60,second:60,millisecond:1000};

function getDiff(start, end)
{
  var duration = "", start = moment(start), end = moment(end);
  units.forEach(function(unit,index){
    var diff = Math.abs(end.diff(start,unit));
    if(unitmapping[unit]) diff = (diff%unitmapping[unit]).toFixed(0);
    duration += diff + " " 
                + unit
                + (diff==1 ? "":"s")
                + (index!=units.length-1 ? " : ":"");
  });
  return duration;
}

console.log(getDiff("2018-07-27T14:56:33.843Z","2018-07-27T14:56:33.763Z"));
console.log(getDiff("2018-07-26T14:56:33.843Z","2018-07-27T14:56:33.763Z"));
console.log(getDiff("2018-07-27T14:46:33.843Z","2018-07-27T14:56:33.763Z"));
console.log(getDiff("2018-07-27T14:56:33.843Z","2048-07-29T14:56:33.763Z"));
console.log(getDiff("2018-09-27T14:56:33.843Z","2048-07-29T14:56:33.763Z"));