moment.js get current time in milliseconds?
Asked Answered
G

7

144
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.

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.

Growler answered 11/10, 2016 at 15:16 Comment(4)
var timeInMilliseconds = moment().valueOf(); See jsfiddle.net/rc1s2ek0Hirst
this is since the epoch....I need todays time in milliseconds not the epoch.Growler
You're right, but your question states you want the time in milliseconds...Hirst
Easiest is probably to get the difference of epoch of now minus epoch of start of todayAbjuration
B
220

From the docs: 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


moment().valueOf()

for the current unix timestamp

Bamberg answered 11/10, 2016 at 15:23 Comment(0)
S
48

See this link http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/

valueOf() is the function you're looking for.

Editing my answer (OP wants milliseconds of today, not since epoch)

You want the milliseconds() function OR you could go the route of moment().valueOf()

Siriasis answered 11/10, 2016 at 15:19 Comment(7)
Thanks but thats not what i'm asking, thats since the epoch year...todays current time in milliseconds.Growler
@code20 ahh, gotcha. Looks like Amy answered it above. You're looking for the milliseconds() function or you could create an empty moment and use the valueOf function. I revised my answer above. momentjs has really good documentation on there stuff and it's pretty organized. You can do some pretty neat stuff with it. You should look over it if you get a chance!Siriasis
Amys answer is incorrect. that is time 0-999 milliseconds, I have read a good amount of the docs and searched. Really can not figure this one out.Growler
@code20 You're looking to get the milliseconds of todays date? I'm a little confused. You can't use the milliseconds epoch time?Siriasis
@code20 you really do need to be more specific on what you're wanting. So far you've received answers for ms since the epoch, or the "current time's ms", which was what you requested. Milliseconds since midnight is quite different.Dracula
You see how my code returns TODAYS time in milliseconds? That's what I need. I need it to call another function in milliseconds to calculate top height for a calendar app. Can not use the epoch. Need todays time formated in milliseconds.Growler
Looks like the OP deleted his user account. Sigh.Dracula
C
31
var timeArr = moment().format('x');

returns the Unix Millisecond Timestamp as per the format() documentation.

Corvine answered 1/3, 2017 at 22:15 Comment(3)
Be careful, it return the timestamp in millisecond but as a String, prefere moment().valueOf() that return a numberLubberly
Try Number(moment().format('x'))Metabolite
@Lime or just add + shorthand. +moment().format('x')Manipulator
D
15

You could subtract the current time stamp from 12 AM of the same day.

Using current timestamp:

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

Using arbitrary day:

moment(someDate).valueOf() - moment(someDate).startOf('day').valueOf()
Discoverer answered 1/6, 2018 at 15:48 Comment(0)
G
1

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.

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>
Godly answered 17/5, 2017 at 20:26 Comment(0)
B
0

Since this thread is the first one from Google I found, one accurate and lazy way I found is :

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 !

Brassy answered 23/11, 2019 at 20:1 Comment(0)
P
0
moment().toDate().getTime()

Hope that helps!

Peale answered 22/4 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.