Introduction
Dates and times are an important part of many web applications. Websites use them for displaying timestamps, scheduling events, managing deadlines, recording transactions, creating calendars, and much more.
JavaScript provides a built-in Date object that allows developers to create, read, modify, and format dates and times.
In this article, we will explore the JavaScript Date object, its syntax, commonly used methods, and practical examples.
What Is the JavaScript Date Object?
The JavaScript Date object is a built-in object used for working with dates and times.
A new Date object can be created using the Date() constructor:
let currentDate = new Date();
console.log(currentDate);
This creates a Date object containing the current date and time.
JavaScript internally represents a Date as a numeric value based on milliseconds from the Unix epoch, which begins on January 1, 1970 UTC.
**
Creating a Date in JavaScript**
There are several ways to create a Date object.
Creating the Current Date
The simplest approach is:
let date = new Date();
console.log(date);
The result represents the current date and time.
Creating a Date from a String
You can also provide a date string:
let date = new Date("January 15, 2026");
console.log(date);
This creates a Date object representing the specified date.
For reliable cross-environment parsing, standardized date-time formats such as ISO 8601 are generally preferable:
let date = new Date("2026-01-15T10:30:00Z");
console.log(date);
Date Object Methods in JavaScript
JavaScript provides many methods for retrieving and modifying parts of a date.
getFullYear()
The getFullYear() method returns the year.
let date = new Date();
console.log(date.getFullYear());
getMonth()
The getMonth() method returns the month as a number from 0 to 11.
January is represented by 0, while December is represented by 11.
let date = new Date();
console.log(date.getMonth());
If you need the usual month number from 1 to 12:
console.log(date.getMonth() + 1);
getDate()
The getDate() method returns the day of the month.
let date = new Date();
console.log(date.getDate());
For example, if the date is January 15, the method returns 15.
getDay()
The getDay() method returns the day of the week as a number from 0 to 6.
let date = new Date();
console.log(date.getDay());
Here, Sunday is 0 and Saturday is 6.
getHours()
The getHours() method returns the hour based on the local time.
let date = new Date();
console.log(date.getHours());
getMinutes()
The getMinutes() method returns the minutes.
let date = new Date();
console.log(date.getMinutes());
getSeconds()
The getSeconds() method returns the seconds.
let date = new Date();
console.log(date.getSeconds());
Setting Date Values
JavaScript also provides methods for changing individual parts of a Date object.
For example:
let date = new Date();
date.setFullYear(2030);
date.setMonth(5);
date.setDate(20);
console.log(date);
Common setter methods include:
setFullYear()
setMonth()
setDate()
setHours()
setMinutes()
setSeconds()
These methods are useful when applications need to modify or calculate dates.
Formatting Dates in JavaScript
Dates can be displayed in different formats depending on the requirements of an application.
toDateString()
The toDateString() method returns the date portion as a readable string.
let date = new Date();
console.log(date.toDateString());
A result may look similar to:
Thu Jan 15 2026
toISOString()
The toISOString() method returns a date in ISO 8601 format.
let date = new Date();
console.log(date.toISOString());
A typical result looks like:
2026-01-15T10:30:00.000Z
This format is particularly useful when exchanging dates between applications and APIs.
toLocaleString()
The toLocaleString() method produces a date and time representation based on a locale.
let date = new Date();
console.log(date.toLocaleString());
The displayed format can vary according to the user's regional settings.
toLocaleDateString()
If you only need the date portion, you can use:
let date = new Date();
console.log(date.toLocaleDateString());
This is useful when the time is not required.
Getting a Timestamp
JavaScript provides Date.now() for getting the current timestamp.
let timestamp = Date.now();
console.log(timestamp);
The result is the number of milliseconds elapsed since January 1, 1970 UTC.
Timestamps are commonly used when comparing dates, recording events, or working with APIs.
Converting a Timestamp to a Date
A timestamp can be converted back into a Date object:
let timestamp = Date.now();
let date = new Date(timestamp);
console.log(date);
This creates a Date object representing the point in time represented by the timestamp.
Creating a Custom Date Format
Sometimes an application needs a specific date format.
For example:
let date = new Date();
let formattedDate =
${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")};
console.log(formattedDate);
This produces a format similar to:
2026-01-15
The + 1 is required for the month because JavaScript numbers months from 0 through 11.
Using Intl.DateTimeFormat()
For more flexible locale-aware formatting, JavaScript provides Intl.DateTimeFormat.
let date = new Date();
let formatter = new Intl.DateTimeFormat("en-IN");
console.log(formatter.format(date));
You can also specify options to control how the date is displayed:
let date = new Date();
let formatter = new Intl.DateTimeFormat("en-IN", {
day: "2-digit",
month: "long",
year: "numeric"
});
console.log(formatter.format(date));
This approach is useful when an application needs dates to appear naturally for users from different regions.
JavaScript Date Example
Here is a simple example that displays the current date:
let today = new Date();
console.log("Year:", today.getFullYear());
console.log("Month:", today.getMonth() + 1);
console.log("Day:", today.getDate());
console.log("Time:", today.toLocaleTimeString());
This example extracts different parts of the current date and displays the current time.
*Common Uses of JavaScript Date
*
The Date object is useful in many types of applications, including:
Digital clocks
Calendars
Event scheduling
Appointment systems
Countdown timers
Online booking systems
Transaction timestamps
Activity logs
Reports and dashboards
Date-based filtering
Important Things to Remember
When working with JavaScript dates, keep these points in mind:
getMonth() starts at 0, so January is 0.
getDate() returns the day of the month, not the day of the week.
getDay() returns the day of the week.
Date.now() returns milliseconds since the Unix epoch.
toISOString() uses UTC.
Locale-based methods can produce different output depending on the user's locale.
Date and time calculations should account for time zones when an application operates across different regions.
*Conclusion
*
The JavaScript Date object provides a built-in way to work with dates and times. Developers can use it to create dates, retrieve individual date components, modify values, generate timestamps, and format dates for users.
Methods such as getFullYear(), getMonth(), getDate(), toDateString(), toISOString(), and toLocaleString() cover many common date-handling requirements.
Once you understand how the Date object works, handling dates and times becomes much easier when building real-world JavaScript applications.
Top comments (0)