To get today’s date and the current time in JavaScript, use the Date()
constructor.
For example, the code snippet below constructs a date with the Date()
constructor, stores it in a let
variable, then outputs it to the browser’s developer console with the console.log()
method.
// Construct a date and store it in a variable
let today = new Date();
// Reference the date and output it to the console
console.log(today);
Go ahead and give this code snippet a try. To do so, fire up your browser’s developer console, copy and paste the code in, then hit the Enter
key on your keyboard. If you’re reading this from your phone or tablet, here’s a screenshot of my console:
The newly-created Date
object holds the date and the time at the moment of instantiation.
Working With the Date Object
Once you’ve obtained today’s date and stored it as a Date
object in a constant or variable, you can use a number of JavaScript methods to extract bits and pieces of the data in it.
As a developer, whether front or backend, it’s very useful to learn how to extract data from the Date
object. Once you’ve mastered this, you will be able to perform calculations and implement conditional logic in your JavaScript application.
Extract the Day of the Week From the Date Object
To get the day of the week from the date object, use the Date.getDay()
method.
// Construct a date and store it in a variable
let today = new Date();
// Get the day of the week and output it to the console
console.log(today.getDay());
The Date.getDay()
method returns a number for the day of the week:
0
for Sunday1
for Monday2
for Tuesday3
for Wednesday4
for Thursday5
for Friday6
for Saturday
Pay attention to the response of the method. It’s a common mistake for developers to expect Sunday to have a value of 7
when it should really have a value of 0
.
Extract the Month of the Year From the Date Object
To get the month from the date object, use the Date.getMonth()
method.
// Construct a date and store it in a variable
let today = new Date();
// Get the month of the year and output it to the console
console.log(today.getMonth());
The Date.getMonth()
method returns a number for the month of the year:
0
for January1
for February2
for March3
for April4
for May5
for June6
for July7
for August8
for September9
for October10
for November11
for December
Pay attention to the response of the method. It’s a common mistake for developers to expect the months to have values from 1
to 12
instead of 0
to 11
.
Extract the Year From the Date Object
To get the month from the Date
object, use the Date.getFullYear()
method.
// Construct a date and store it in a variable
let today = new Date();
// Get the month of the year and output it to the console
console.log(today.<span style="background-color: inherit; font-family: inherit; font-size: inherit; white-space: pre;">getFullYear</span><span style="background-color: inherit; font-family: inherit; font-size: var(--global--font-size-base);">());</span>
The Date.getFullYear()
method returns a numeric value for the year (for example, 2022
).
Note: Some tutorials from a few years ago tell you to use the Date.getYear()
method instead. This method is deprecated and should no longer be used in your code, as it may cause browser support problems in the future.
Convert the Date to Milliseconds
To convert the date and time from the Date
object into the number of milliseconds elapsed before/after 00:00:00 UTC on January 1st, 1970 (also known as the ECMAScript epoch), use the Date.parse()
method.
// Construct a date and store it in a variable
let today = new Date();
// Get the month of the year and output it to the console
console.log(Date.<span style="background-color: inherit; font-family: inherit; font-size: inherit; white-space: pre;">parse</span><span style="background-color: inherit; font-family: inherit; font-size: var(--global--font-size-base);">(today));</span>
Notice the syntax. The constant or variable containing the date object must be passed as an argument to the Date.parse()
method.