What's the best way to obtain current time as a number in JavaScript?

I’m working on a JavaScript project and I need to get the current date and time as a single numeric value. I’ve heard about Unix timestamps but I’m not sure how to generate one in JavaScript. I want something that gives me just one number representing right now, similar to what other programming languages call epoch time. I tried looking for answers, but there are so many different date methods in JS that I’m getting confused about which one to use. Can someone show me the simplest approach to get the current moment as a timestamp? I need this for comparing different times in my application and storing time data efficiently.

Another option is using new Date().getTime() which does the same thing as Date.now() but through a different approach. Both return milliseconds since Unix epoch (January 1, 1970). I’ve been using Date.now() in my projects for years and it’s reliable across all browsers. One thing to keep in mind is that JavaScript timestamps are in milliseconds, not seconds like some other languages, so if you’re interfacing with backend systems that expect seconds you’ll need to divide by 1000. The numeric value you get is perfect for comparisons and sorting since higher numbers always represent later times.

There’s also +new Date() which uses the unary plus operator to convert a Date object to its numeric timestamp value. I discovered this method accidentally while debugging some legacy code and it actually performs quite well. The plus operator essentially calls valueOf() on the Date object, returning the same millisecond timestamp as the other methods mentioned. While Date.now() is more readable, this approach can be handy when you’re already working with Date objects in your code. Performance-wise, all three methods are virtually identical in modern browsers, so it really comes down to code readability and your specific use case.

hey, you can just do Date.now(). it gives you the current timestamp in milliseconds since 1970. super easy and it works all over, no need to bother with creating Date objects or anything like that.