Javascript moment.js 以毫秒为单位获取当前时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39980722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
moment.js get current time in milliseconds?
提问by Cristian Cavalli
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. 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.
此代码以毫秒为单位返回 TODAYS 时间。我需要它以毫秒为单位调用另一个函数......不能使用纪元。需要以毫秒为单位的今天时间。上午 9:00 = 3.24e+7 毫秒 晚上 9:00 = 6.84e+7 毫秒。
回答by Cristian Cavalli
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()
moment(...).valueOf()
to parse a preexisting date and convert the representation to a unix timestamp
解析预先存在的日期并将表示形式转换为 unix 时间戳
moment().valueOf()
moment().valueOf()
for the current unix timestamp
对于当前的 unix 时间戳
回答by James
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()
回答by Alexandr Golovchuk
var timeArr = moment().format('x');
returns the Unix Millisecond Timestamp as per the format() documentation.
根据format() 文档返回 Unix 毫秒时间戳。
回答by Jeremy Quick
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()
回答by 0xCDCDCDCD
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() 或其他任何东西!
回答by JLRishe
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. I doubt it's something that people have a need for very often.
您可以只获取各个时间分量并计算总数。您似乎希望 Moment 已经为您精心打包了此功能,但事实并非如此。我怀疑这是人们经常需要的东西。
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>
回答by Amy
To get the current time's milliseconds, use http://momentjs.com/docs/#/get-set/millisecond/
要获取当前时间的毫秒数,请使用http://momentjs.com/docs/#/get-set/millisecond/
var timeInMilliseconds = moment().milliseconds();