DEV Community

Cover image for JavaScript Interview Question #34: Different ways to get the current date in JS
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com on

10 4

JavaScript Interview Question #34: Different ways to get the current date in JS

js-test-34

How do you prefer to get the current date in JS? What will be logged to the console?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Both new Date() and Date.now() in JavaScript return us the current date and time.

The difference between them is that new Date() returns the Date object and Date.now() returns the number of milliseconds elapsed from the midnight of Jan 1, 1970.

If you need to compare two dates in different formats, you can always get the number of milliseconds from any Date object using the built-in getTime() function.

Otherwise, you won’t have any luck comparing a number to an object.


ANSWER: false will be logged to the console.

Learn Full-Stack JavaScript

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!