将时间差与一天结束时间的整数四舍五入为moment.js? [英] Rounding time duration of difference to end of day to whole hours in momentjs?

查看:228
本文介绍了将时间差与一天结束时间的整数四舍五入为moment.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是轮次的重复项向上/向下舍入一秒到最接近的分钟-因为首先我不想四舍五入到最接近的分钟;第二,我不想无条件地舍入 ,而仅当与.endOf('day')的差额不是我原本期望的整个小时数时;第三,我要舍入moment.duration,而不是moment.

This is not a duplicate of round up/ round down a momentjs moment to nearest minute - because first I don't want to round to nearest minute; second, I don't want to round unconditionally, but only when the difference to .endOf('day') is not the whole hours I'd otherwise expect; and third, I want to round a moment.duration, not a moment.

说我有一个日期/时间戳,"2017-02-17 21:00:00",我想找到一天结束时有多少小时.从精神上讲,如果我想到晚上9点,我认为它离午夜3小时,这就是我想从momentjs获得的答案.这就是我正在做的(Firefox中的Javascript Web Console):

Say I have a date/time stamp, "2017-02-17 21:00:00" and I want to find how many hours there are to the end of day. Mentally, if I think of 9 o'clock in the evening, I consider it 3 hours away from midnight, and that is the answer I'd want to obtain from momentjs. This is what I am doing (Javascript Web Console in Firefox):

var m1 = moment('2017-02-17 21:00:00');
<- undefined

var m2 = moment(m1).endOf('day');
<- undefined

m1
<- Object { _isAMomentObject: true, _i: "2017-02-17 21:00:00", _f: "YYYY-MM-DD HH:mm:ss", _isUTC: false, _pf: Object, _locale: Object, _a: Array[7], _d: Date 2017-02-17T20:00:00.000Z, _isValid: true, _z: null }

m2
<- Object { _isAMomentObject: true, _i: "2017-02-17 21:00:00", _f: "YYYY-MM-DD HH:mm:ss", _isUTC: false, _pf: Object, _locale: Object, _z: null, _a: Array[7], _d: Date 2017-02-17T22:59:59.999Z, _isValid: true }

var mdiff = moment(m2).diff(moment(m1))
<- undefined

mdiff
<- 10799999

var mddur = moment.duration(moment(m2).diff(moment(m1)))
<- undefined

mddur
<- Object { _isValid: true, _milliseconds: 10799999, _days: 0, _months: 0, _data: Object, _locale: Object }

到目前为止,很好-现在,要格式化持续时间,我按照持续时间格式设置•问题#1048· moment/moment·GitHub );请注意,我想使用相同的函数来获取大于24小时的正确持续时间来计算该时间-即使此示例的持续时间短于24小时,所以我使用以下函数:

So far, so good - now, to format the duration, I go as per Get the time difference between two datetimes (also duration formatting · Issue #1048 · moment/moment · GitHub); note that I want to use the same function I'd use to get correct durations larger than 24 hours to calculate this - even if this particular example has a duration shorter than 24h, so I use this:

Math.floor(mddur.asHours()) + moment.utc(mddur.asMilliseconds()).format(":mm:ss")
<- "2:59:59"

因此,我想在这里获得答案"3:00:00",而不是"2:59:59"-注意,我仍然希望原样是"2:59:58",并且 not 四舍五入.

So, here I's want to obtain the answer "3:00:00" here, not "2:59:59" - though note, I'd still want "2:59:58" as is, and not rounded up.

我想,如果我们的持续时间(以毫秒为单位)为10799999,即10799999/1000 = 10799.999000秒,那么如果我们的持续时间具有999毫秒的毫秒余数,那么我只想进行四舍五入.

I guess, if our duration in ms is 10799999, that is 10799999/1000=10799.999000 seconds, so if we have a duration that has millisecond remainder of 999 milliseconds, only then I would want a round up.

通过moment.js实现此目标的推荐方法是什么?

What would be the recommended way of achieving this with moment.js?

推荐答案

好吧,我最终制作了一个函数,该函数可以实现我想要的功能,

Ok, I ended up making a function that does what I want, here it is:

function padDigits(number, digits) { // SO:10073699
  return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
}

var durationToHoursStr = function(induration) {
  // copy the duration
  thisduration = moment.duration(induration);
  // to get seconds, we divide milliseconds with 1000;
  // check if the remainder of division of milliseconds with 1000 is 999:
  var remainder = thisduration.asMilliseconds() % 1000;
  //console.log("remainder", remainder);
  // only round up if the remainder is 999:
  if (remainder >= 999) {
    // in call moment.duration(Number), Number is interpreted as "length of time in milliseconds" (https://momentjs.com/docs/#/durations/)
    // so just add 1 ms, since anyways the previous remainder is 999, should be enough to flow over
    thisduration.add(moment.duration(1)); // should be in-place replacement - mutable
    //console.log("thisduration", thisduration);
  }
  return String(Math.floor(thisduration.asHours())) + ":" + padDigits(thisduration.minutes(), 2) + ":" + padDigits(thisduration.seconds(), 2);
};

var m1 = moment('2017-02-17 21:00:00');
var m2 = moment(m1).endOf('day');

// mdiff is int, total number of milliseconds, here 10799999
var mdiff = moment(m2).diff(moment(m1))

// mddur is moment.duration, also has the same amount of milliseconds, 
// mddur.asMilliseconds() = 10799999, mddur.asSeconds() = 10799.999, mddur.asHours() = 2.9999997222222223
var mddur = moment.duration(moment(m2).diff(moment(m1)));

var mddur_hoursString = durationToHoursStr(mddur);
console.log(mddur_hoursString); // prints "3:00:00"

var m3 = moment('2017-02-17 23:59:59');
var mddur3 = moment.duration(moment(m3).diff(moment(m1)));
var mddur3_hoursString = durationToHoursStr(mddur3);
console.log(mddur3_hoursString); // prints "2:59:59" - as it should, since we didn't really demand the difference to the end of day

这篇关于将时间差与一天结束时间的整数四舍五入为moment.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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