通过使用moment.js将秒持续时间格式化为DD:HH:mm格式 [英] Formatting seconds duration into DD:HH:mm format by using moment.js

查看:575
本文介绍了通过使用moment.js将秒持续时间格式化为DD:HH:mm格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算以秒为单位的持续时间,格式为 DD-HH-mm格式.

I'm trying to count calculate a duration from seconds into a DD-HH-mm format.

我的JavaScript代码:

My javascript code:

var seconds = 120;
var result = moment.utc(seconds*1000).format('DD:HH:mm');

我的代码应返回以下内容: 00:00:02 (DD:HH:MM),但它返回的内容是: 01:00:02 (DD:HH:MM)

My code should return something like this: 00:00:02 (DD:HH:MM) but it returns that: 01:00:02 (DD:HH:MM)!

我确定那是因为我的当地时间,但是通常如何确定1小时间隔?

I'm sure that's because of my local time, but how to fix the 1 hour interval in general?

推荐答案

moment.utc创建一个带有时区设置为GMT/UTC的moment.js对象.使用持续时间的日期时,您需要允许从1开始的日期,而不是零.另外,如果持续时间为32天或更长时间,则天数"将重置为1.

moment.utc creates a moment.js object with a timezone set to GMT/UTC. When using a date for a duration, you need to allow for the date starting from 1, not zero. Also, if the duration is 32 days or longer, the "days" will reset to 1.

Moment.js也具有持续时间,但是,它们不支持其他格式而不是"人性化"或转换为特定单位.

Moment.js also has durations, however, they don't support formatting other than "humanize" or converting to particular units.

如果工期少于32天,则可以使用任何年份的1月1日开始的日期,前提是您要处理的天数不为零索引(即从天数中减去1).

If your durations are less than 32 days, you can use a date starting from 1 January in any year provided you deal with the day number not being zero indexed (i.e. subtract 1 from the day).

因此,使用moment.js来获取所需的格式要比仅格式化日期还要多,因此您需要执行一系列步骤,因此请考虑编写一个函数.在这种情况下,普通的JS函数只需要片刻即可完成,它将处理32天或更长时间,并且不受诸如夏令时和时区之类的日期变化的影响.

So getting your required format with moment.js is a bit more work than just formatting a date, you'll need a sequence of steps so consider writing a function. A plain JS function is no more work than a moment one in this case, it will handle durations 32 days or longer and is not affected by Date vagaries like daylight saving and timezones.

var secs = 120;

// Using a duration
var m = moment.duration(secs * 1000);
console.log(m);
console.log(m.humanize());
console.log(m.asMinutes());

// Using a date and seconds value
var x = moment.utc(secs*1000);
// Generated date
console.log(x.format());
// Get the days separately
var dayNum = x.format('D') - 1;
// Format with hours and minutes
console.log(('0'+dayNum).slice(-2) + x.format(':HH:mm'))

// Function using moment.js
function myFormat(secs) {
  var x = moment.utc(secs*1000);
  var dayNum = x.format('D') - 1;
  return ('0'+dayNum).slice(-2) + x.format(':HH:mm');
}

// Function without using a Date
function duration(secs) {
  function z(n){return ('0'+n).slice(-2)}
  return z((secs/8.64e4|0)) 
         + ':' + z((secs%8.64e4)/3.6e3|0)
         + ':' + z((secs%3.6e3)/60|0)
//         + ':' + z(secs%60);
}

console.log(duration(secs));

// Some other tests
var min = 60;
var hr  = 60*min; // 3,600
var day = 24*hr;  // 86,400

//2 days 17 hours 53 minutes and 08 seconds
var d = 2*day + 17*hr + 53*min + 8;
//0 days 1 hour 2 minutes and 1 second 
var e = 0*day + 1*hr + 2*min + 1;
// 48 days 21 hours 15 minutes
var f = 48*day + 21*hr + 15*min;
 
[120, d, e, f].forEach(function(d) {
  console.log(d + ' seconds');
  console.log('Plain js:  ' + duration(d));
  console.log('Moment fn: ' + myFormat(d));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

该格式似乎模棱两可,我想许多人会将其解释为HH:mm:ss而不是DD:HH:mm.

The format seems ambiguous, I think many would interpret it as HH:mm:ss rather than DD:HH:mm.

这篇关于通过使用moment.js将秒持续时间格式化为DD:HH:mm格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆