When you build websites or web applications, you need to work with dates and time in JavaScript. For example, you may want to show today’s date, calculate someone’s age, display order time, or set a countdown timer. In all these cases, the JavaScript Date object play a very important role.
In this guide, I will explain the JavaScript Date object step by step with simple practical examples.
What is a JavaScript Date Object?
The JavaScript Date object helps you to create and manage date and time values in JavaScript code.
In simple words, it works like a built-in digital clock and calendar inside your program.
JavaScript stores time in milliseconds from January 1, 1970. But you do not need to handle milliseconds directly in most cases.
How to Create a JavaScript Date
1️⃣ Creating Current Date and Time
let currentDate = new Date();
console.log(currentDate);
This returns the current system date and time.
Example- If today is February 17, 2026, at 10:30 AM, it shows that.
2️⃣ Creating a Specific Date
let customDate = new Date(2026, 0, 1);
console.log(customDate);
Here:
- 2026 = Year
- 0 = January
- 1 = Day
Important – In the JavaScript Date object, months start from 0.
0 = January
11 = December
3️⃣ Creating Date from String
let dateFromString = new Date("2026-12-25");
console.log(dateFromString);
This creates December 25, 2026. This format is simple and commonly used in projects.
JavaScript Date Methods (Quick Overview Table)
Below is a table of commonly used JavaScript Date methods with their descriptions.
| Method | What It Does | Example Output |
| new Date() | Creates a new date object | Current date & time |
| getFullYear() | Returns the year | 2026 |
| getMonth() | Returns month (0–11) | 0 = January |
| getDate() | Returns day of month (1–31) | 17 |
| getDay() | Returns weekday (0–6) | 1 = Monday |
| getHours() | Returns hour (0–23) | 14 |
| getMinutes() | Returns minutes | 30 |
| getSeconds() | Returns seconds | 45 |
| setFullYear() | Sets year | Changes year |
| setMonth() | Sets month | Changes month |
| setDate() | Sets day of month | Changes day |
| toLocaleDateString() | Formats date | 2/17/2026 |
| toLocaleTimeString() | Formats time | 10:30 AM |
| toLocaleString() | Formats date & time | 2/17/2026, 10:30 AM |
Now, let’s understand each important JavaScript Date method in detail.
Getting Date Values (Detailed Explanation)
Let’s create a date –
let today = new Date();
Now we will use Date methods to extract information.
➤ getFullYear()
today.getFullYear();
This date method returns the current year.
Example – If today is 2026, it returns 2026.
➤ getMonth()
today.getMonth();
This method returns the month number from 0 to 11.
If it returns 1, it means February.
To get the actual month number –
today.getMonth() + 1;
➤ getDate()
today.getDate();
This method returns the day of the month (1–31).
Example – If today is February 17, it returns 17.
➤ getDay()
today.getDay();
Returns weekday number –
0 = Sunday
1 = Monday
2 = Tuesday
Example – If today is Monday, it returns 1.
➤ getHours()
today.getHours();
Returns the hour in 24-hour format. If it returns 14, it means 2 PM.
➤ getMinutes()
today.getMinutes();
Returns the minutes value.
➤ getSeconds()
today.getSeconds();
Returns the seconds value.
Setting Date Values (Detailed Explanation)
You can also modify a JavaScript Date object.
➤ setFullYear()
let date = new Date();
date.setFullYear(2030);
console.log(date);
This changes the year to 2030.
➤ setMonth()
date.setMonth(5);
This sets the month to June (because 0 = January).
➤ setDate()
date.setDate(15);
This changes the day of the month to 15.
Formatting Dates in JavaScript
Raw date output looks long and messy. So formatting helps.
➤ toLocaleDateString()
let date = new Date();
console.log(date.toLocaleDateString());
Output example – 2/17/2026
➤ toLocaleTimeString()
console.log(date.toLocaleTimeString());
Output – 10:30:15 AM
➤ toLocaleString()
console.log(date.toLocaleString());
Output – 2/17/2026, 10:30:15 AM
This method is very useful in dashboards and reports.
Date Calculations Example
Calculate Difference Between Two Dates
let date1 = new Date("2026-01-01");
let date2 = new Date("2026-01-10");
let difference = date2 - date1;
let days = difference / (1000 * 60 * 60 * 24);
console.log(days);
Output- 9
You can use this for –
- Countdown timers.
- Subscription expiry.
- Event tracking.
Common Mistakes in JavaScript Date
1️⃣ Month Starts from 0
Many beginners forget this.
2️⃣ Timezone Confusion
Different users may see different time.
3️⃣ Invalid Date
Wrong format returns “Invalid Date”.
Always validate input.
Why JavaScript Date Is Important
The JavaScript Date object is important because almost every web application uses date and time.
Examples –
- Show publish date on blog.
- Show last login time.
- Calculate age.
- Display order date.
- Run countdown timer.
Without JavaScript Date, you cannot build real-world applications properly.
Final Thoughts
The JavaScript Date object helps you manage date and time easily in JavaScript. You can create dates, get specific values like year or month, change them, format them, and even calculate differences.
At first, it may feel confusing. But once you practice small examples, it becomes simple.
Start experimenting with –
- Showing current date.
- Calculating age.
- Creating a small countdown.
Practice makes everything clear. Keep learning and keep coding.
You Might Also Like –
1- this Keyword in JavaScript
2- JavaScript:void(0)
3- location.reload(true)

