简体   繁体   English

moment.js 以毫秒为单位获取当前时间?

[英]moment.js get current time in milliseconds?

var timeArr = moment().format('HH:mm:ss').split(':');

var timeInMilliseconds = (timeArr[0] * 3600000) + (timeArr[1] * 60000);

This solution works, test it, but I'd rather just use the moment api instead of using my own code.这个解决方案有效,测试它,但我宁愿只使用 moment api 而不是使用我自己的代码。

This code returns TODAYS time in milliseconds.此代码以毫秒为单位返回 TODAYS 时间。 I need it to call another function in milliseconds...Can not use the epoch.我需要它以毫秒为单位调用另一个函数......不能使用纪元。 Need today's time formated in milliseconds.需要以毫秒为单位的今天时间。 9:00am = 3.24e+7 milliseconds 9:00pm = 6.84e+7 milliseconds.上午 9:00 = 3.24e+7 毫秒 晚上 9:00 = 6.84e+7 毫秒。

From the docs:http://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/来自文档:http ://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/

So use either of these:因此,请使用以下任一方法:


moment(...).valueOf()

to parse a preexisting date and convert the representation to a unix timestamp解析预先存在的日期并将表示形式转换为 unix 时间戳


moment().valueOf()

for the current unix timestamp对于当前的 unix 时间戳

See this link http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/请参阅此链接http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/

valueOf() is the function you're looking for. valueOf()是您正在寻找的函数。

Editing my answer (OP wants milliseconds of today, not since epoch)编辑我的答案(OP 需要今天的毫秒数,而不是自纪元以来)

You want the milliseconds() function OR you could go the route of moment().valueOf()你想要milliseconds()函数或者你可以走moment().valueOf()的路线

var timeArr = moment().format('x');

根据format() 文档返回 Unix 毫秒时间戳。

You could subtract the current time stamp from 12 AM of the same day.您可以从同一天的上午 12 点减去当前时间戳。

Using current timestamp:使用当前时间戳:

moment().valueOf() - moment().startOf('day').valueOf()

Using arbitrary day:使用任意日期:

moment(someDate).valueOf() - moment(someDate).startOf('day').valueOf()

You can just get the individual time components and calculate the total.您可以只获取各个时间分量并计算总数。 You seem to be expecting Moment to already have this feature neatly packaged up for you, but it doesn't.您似乎希望 Moment 已经为您精心打包了此功能,但事实并非如此。 I doubt it's something that people have a need for very often.我怀疑这是人们经常需要的东西。

Example:示例:

 var m = moment(); var ms = m.milliseconds() + 1000 * (m.seconds() + 60 * (m.minutes() + 60 * m.hours())); console.log(ms);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Since this thread is the first one from Google I found, one accurate and lazy way I found is :由于此线程是我从 Google 找到的第一个线程,因此我发现的一种准确而懒惰的方法是:

const momentObject = moment().toObject();
// date doesn't exist with duration, but day does so use it instead
//   -1 because moment start from date 1, but a duration start from 0
const durationCompatibleObject = { ... momentObject, day: momentObject.date - 1 };
delete durationCompatibleObject.date;

const yourDuration = moment.duration(durationCompatibleObject);
// yourDuration.asMilliseconds()

now just add some prototypes (such as toDuration()) / .asMilliseconds() into moment and you can easily switch to milliseconds() or whatever !现在只需将一些原型(例如 toDuration())/ .asMilliseconds() 添加到 moment 中,您就可以轻松切换到 milliseconds() 或其他任何东西!

要获取当前时间的毫秒数,请使用http://momentjs.com/docs/#/get-set/millisecond/

var timeInMilliseconds = moment().milliseconds();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM