如何使用 moment.js 从 unix 时间戳中获取剩余的小时、分钟和秒? [英] How to use moment.js to get remaining hours, minutes, and seconds from a unix timestamp?

查看:99
本文介绍了如何使用 moment.js 从 unix 时间戳中获取剩余的小时、分钟和秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定未来的 unix 时间戳,我如何使用 Moment.js 获取到那时剩余的小时、分钟和秒?

Given a unix timstamp in the future, how can I use Moment.js to get the hours, minutes, and seconds remaining until then?

例如:

now  = 1589252712837
time = 1589356202907

// This is the line I do not know
res = moment(time - now)

console.log(res)
// 23 hours, 12 minutes, 3 seconds

我相信我可以做这样的事情,但有没有一种原生的方式来做到这一点?:

I believe I can do something like this, but is there a native way to do it?:

now  = 1589252712837
time = 1589356202907
remaining = time - now

hour = moment(remaining).hour()
minute = moment(remaining).minute()
second = moment(remaining).second()

console.log(`${hour} hours, `${minute} minutes, ${seconds} seconds`)
// 23 hours, 12 minutes, 3 seconds

推荐答案

请注意,您拥有的示例代码是时间偏移:remaining/1000/60/60~=28,而不是 23.您需要使用 moment.utc.不过我不建议这样做,因为你会遇到其他问题,比如处理几天和几个月.

Note that the example code you have is being time offset: remaining/1000/60/60~=28, not 23. You need to use moment.utc. I don't recommend doing that though, because you'll encounter other problems like dealing with days and months.

至于原生"支持,请参阅此处的详细讨论:https://github.com/moment/moment/issues/463
TL;DR:从 2012 年至今讨论过.而且,在文档中,他们指向 moment-duration-format 插件.如果您想要接近本机"支持的东西,请查看此插件:
https://github.com/jsmreese/moment-duration-format

As far as "native" support, see long discussion here: https://github.com/moment/moment/issues/463
TL;DR: Discussed from 2012 until now. And, in the docs they point at the moment-duration-format plugin. Look at this plugin if you want something close to "native" support:
https://github.com/jsmreese/moment-duration-format

老实说,在查看情况后,如果是我,我可能只会使用 moment-持续时间格式,或者只使用humanize().也许我自己类似于duration._data的生成方式+ Intl.NumberFormat,这就是我猜的moment-duration-format 基本上已经在做.

Honestly after looking at the situation, if it were me I'd probably just use moment-duration-format, or just use humanize(). Maybe roll my own similar to how duration._data is generated + Intl.NumberFormat, which is what I'm guessing moment-duration-format is basically already doing.

我将尽可能列出一些可能的方法:

I'm going to list some possible methods as far as I can see:

now  = 1589252712837
time = 1589356202907
remaining = time - now

// extract key(units),value from parsed ._data property of duration
// no additional dependencies but depends on internal private variable _data
console.log(
Object.entries(moment.duration(remaining,'milliseconds')._data).reverse()
  .flatMap(([unit,value])=>value!==0?`${value} ${unit}`:[]).join(' ')
);

// using moment-duration-format plugin
console.log(moment.duration(remaining,'milliseconds').format('h [hours] m [minutes] s [seconds]'))

// twitter style humanize
console.log(moment.duration(remaining,'milliseconds').humanize());

// hours threshhold is Infinity; never round up to higher units than hours
console.log(moment.duration(remaining,'milliseconds').humanize({h:Infinity}));

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

<!-- moment-duration-format plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/2.3.1/moment-duration-format.min.js"></script>

这篇关于如何使用 moment.js 从 unix 时间戳中获取剩余的小时、分钟和秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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