JS Get Date

JavaScript Get Date Methods: A Beginner's Guide

Once you have created a Date object in JavaScript, you will often need to extract specific pieces of information from it—like the current year, the month, or the exact hour.

JavaScript provides a set of built-in Get Date Methods specifically for extracting this data from a date object.


The Core "Get" Methods

Here is a quick reference table of the most commonly used methods for getting information from a date object:

Method Description Return Value
getFullYear() Get the year A 4-digit number (e.g., 2024)
getMonth() Get the month A number from 0 to 11
getDate() Get the day of the month A number from 1 to 31
getDay() Get the weekday A number from 0 to 6 (Sunday = 0)
getHours() Get the hour A number from 0 to 23
getMinutes() Get the minutes A number from 0 to 59
getSeconds() Get the seconds A number from 0 to 59
getMilliseconds() Get the milliseconds A number from 0 to 999
getTime() Get the time Milliseconds since Jan 1, 1970

Extracting Basic Date Components

Let's look at how to extract the year, month, and day from the current date.

Crucial Reminder: In JavaScript, months are 0-indexed. January is 0, February is 1, and December is 11. If you want the actual human-readable month number, you must add 1 to the result of getMonth()!

Basic Get Methods Example

const d = new Date();

let year = d.getFullYear(); let month = d.getMonth() + 1; // Adding 1 to make it human-readable! let day = d.getDate();

console.log("Year: " + year); console.log("Month: " + month); console.log("Day: " + day);

Note: Never use the deprecated getYear() method, which returns a 2-digit or 3-digit number. Always use getFullYear() to safely get a 4-digit year.


Extracting Time Components

Similarly, you can extract the exact time down to the millisecond using the time-based get methods.

Extracting Time Example

const d = new Date();

let hours = d.getHours(); let minutes = d.getMinutes(); let seconds = d.getSeconds();

console.log("Current Time is " + hours + ":" + minutes + ":" + seconds);


How to Get the Month and Weekday Names

Because getMonth() and getDay() return numbers (e.g., 0 for Sunday, 1 for Monday), developers often want to convert these numbers into actual names.

The easiest way to do this is by creating an Array of names, and using the returned number as the index to select the right name!

Getting the Name of the Weekday

Get Weekday Name Example

// 1. Create an array of weekday names
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

const d = new Date();

// 2. Get the weekday number (0-6) let dayNumber = d.getDay();

// 3. Use the number to pick the correct name from the array let dayName = days[dayNumber];

console.log("Today is " + dayName);

Getting the Name of the Month

We can use the exact same technique for the month!

Get Month Name Example

// 1. Create an array of month names
const months = ["January", "February", "March", "April", "May", "June", 
                "July", "August", "September", "October", "November", "December"];

const d = new Date();

// 2. Use getMonth() to pick the correct name from the array let monthName = months[d.getMonth()];

console.log("The current month is " + monthName);


UTC Date Methods

By default, all the methods above return the date and time based on the user's local time zone (the time configured on their computer or phone).

If your application needs to display the exact same time to users across the world, you should use the UTC Methods. These methods return the date and time in Coordinated Universal Time (UTC).

They are exactly the same as the standard methods, but with UTC added to the name:

Local Time vs UTC Time

const d = new Date();

console.log("Local Hour: " + d.getHours()); console.log("UTC Hour: " + d.getUTCHours());


Exercise

?

If the current date is August 15, 2024, what will new Date().getMonth() return?