Moment.js 24小时制,处理24小时制 [英] Moment.js 24-hour time format, dealing with the 24th hour

查看:311
本文介绍了Moment.js 24小时制,处理24小时制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用momentjs来检查火车旅行的两个时间戳之间以分钟为单位的差异.我用来从中获取数据的API以24小时格式返回时间(momentjs:HH:mm:ss).

如果旅行继续到第二天,则时间显示为例如"24:12:00",这很奇怪.Momentjs无法处理此问题,如果我尝试使用此类值计算时差,则会得到"NaN".

因此,我创建了一个函数,将出现的所有"24"都转换为"00".

  function maintenanceHour(timeString){var splitted = timeString.split(':');if(splitted [0] =='24'){splitted [0] ='00';}返回splitted [0] +':'+ splitted [1] +':'+ splitted [2];} 

如果我使用 diff()函数检查一个时间戳与以"00"为小时的时间戳之间的时差,则无论是当日还是第二天,我都无法区分.因此,例如,如果当前时间是16:00(表示当天的早晨),则差异会在-900左右,而不是直到下一天的24h的正X分钟.

有什么想法如何处理吗?

解决方案

由于您只需要处理一段时间,因此会遇到类似这样的问题.当您只处理一个时间时,矩便是解析时的当前日期.当然,当您将这些时间设置回零时,您会遇到您要描述的问题-结束时间早于开始时间.

这是不直观的,但实际上我可能会使用set的溢出"功能来解决此问题.

将值传递给力矩设置函数( .hours() .minutes()等)时,如果该值超出允许范围,则它将将溢出到下一个单元.您可以在这里利用它来发挥自己的优势.照原样分配后面的时间(可能有24个时间).然后执行以下操作:

  moment.utc('2016-01-01').set({hours:splitted [0],minutes:splitted [1],seconds:splitted [2]}).format() 

作为发生的具体示例:

  moment.utc('2016-01-01').set({hours:24,minutes:32,seconds:12}).format()"2016-01-02T00:32:12Z" 

如您所见,24日将其推入第二天.

听起来您这里所有的都是时间,而不是日期.如果正确,那么我强烈建议您选择一个任意日期作为您的解析日期,并暂时使用UTC模式.

如果在本地模式下使用带有任意日期的时刻,则可能会遇到由于DST转换而导致差异与预期不符的问题.在UTC模式下,这些不会发生.

相反,值得注意的是,如果您确实知道日期,则应该使用它,并在正确的时区(必要时使用时区)进行使用,以便捕获可能导致时差变化的DST转换在相同的两次之间变化.

此外,手动设置日期可以避免可能出现的争用情况,在这种情况下,力矩的第一次调用与力矩的第二次调用位于不同的日期.这是一个错误,以后很难找到.

I am using momentjs to check difference in minutes between two time stamps of a train trip. The API that I'm using to grab the data from returns times in 24 hours format (momentjs: HH:mm:ss).

If the trip continues to the next day, the time is shown as, for example, "24:12:00" which is pretty weird. Momentjs can't deal with this, and if I try to calculate time difference with such values I get "NaN".

So I created a function to convert any "24" occurrence to "00".

function maintainHour (timeString) {
  var splitted = timeString.split(':');
  if(splitted[0] == '24') {
    splitted[0] = '00';
  }

  return splitted[0] + ':' + splitted[1] + ':' + splitted[2];
}

If I use the diff() function to check difference between one timestamp and a timestamp that has "00" as hour, I cannot differ whether it's a timestamp within the current day or the next day. So difference will be something around -900 if current time is 16:00 for example (meaning the morning of this day) rather than positive X minutes until 24h of the upcoming day.

Any idea how to deal with this?

解决方案

Because you are dealing with just a time, you're going to have problems like this. When you deal with just a time, moment assumes the current day when parsing. Of course, when you set those hours back to zero, you get the problem you are describing - the end time is earlier than the start.

This is un-intuitive, but I would maybe actually use the 'overflow' capabilities of set to resolve this issue.

When you pass a value to a moment set function (.hours(), .minutes(), etc), if the value exceeds the allowable range, it will overflow into the next unit. You can use that to your advantage here. Split the later time (the one that could potentially have the 24), as you are. Then do something like this:

moment.utc('2016-01-01')
.set({hours:splitted[0], minutes:splitted[1], seconds:splitted[2]}).format()

As a concrete example of what happens:

moment.utc('2016-01-01').set({hours:24, minutes:32, seconds:12}).format()
"2016-01-02T00:32:12Z"

As you can see, 24 pushed it into the next day.

It sounds like all you have here are times, not dates. If that is correct, then I strongly suggest picking an arbitrary date as your parse date, and using UTC mode for your moments.

If you use moment in local mode with an arbitrary date, you could potentially run into issues where differences are not what you expect due to DST transitions. In UTC mode, those don't happen.

It is worth noting that conversely, if you DO know the date, you should use it, and in the correct time zone (with moment timezone if necessary), so that you do capture DST transitions that may cause the difference in hours to vary between the same two times.

In addition, setting the date manually avoids a possible race condition where the first invocation of moment lands on a different date than the second invocation of moment. That's a bug that will be super hard to find later.

这篇关于Moment.js 24小时制,处理24小时制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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