codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

37 / 65 topics
35JavaScript Sets36JavaScript Maps37JavaScript Dates38JavaScript Symbols
Tutorials/JavaScript/JavaScript Dates
🌐JavaScript

JavaScript Dates

Updated 2026-05-12
30 min read

JavaScript Dates

In this tutorial, we'll explore the Date object in JavaScript, which is essential for handling dates and times. Whether you're building a calendar application or simply need to track timestamps, understanding how to work with dates is crucial.

Introduction

JavaScript provides a built-in Date object that allows you to work with dates and times. This object enables you to perform operations such as creating date instances, retrieving individual components of a date (like year, month, day), setting new values for these components, and formatting dates into readable strings.

Creating Date Instances

You can create a new Date object in several ways:

  1. Using the Current Date and Time:
JavaScript
1const now = new Date();
Output
  1. Using Timestamp (milliseconds since January 1, 1970):
JavaScript
1const timestamp = 1696472880000;
2 const dateFromTimestamp = new Date(timestamp);
Output

Setting Date/Time Components

You can also set individual components of a date using corresponding setter methods:

MethodDescription
setFullYear(year)Sets the year
setMonth(month)Sets the month (0-11)
setDate(day)Sets the day of the month (1-31)
setHours(hours)Sets the hours (0-23)
setMinutes(minutes)Sets the minutes (0-59)
setSeconds(seconds)Sets the seconds (0-59)
setMilliseconds(ms)Sets the milliseconds (0-999)
JavaScript
1const date = new Date();
2date.setFullYear(2024);
3date.setMonth(11); // December is 11
4date.setDate(31);
5date.setHours(23);
6date.setMinutes(59);
7date.setSeconds(59);
8date.setMilliseconds(999);
9
10console.log(date.toISOString());
Output

Tip

When working with dates, always be aware of time zones and consider using libraries like moment.js or date-fns for more robust date manipulation.

Practical Example

Let's create a simple application that calculates the number of days between two dates:

JavaScript
1// Function to calculate days between two dates
2function getDaysBetweenDates(date1, date2) {
3const oneDay = 24 * 60 * 60 * 1000; // milliseconds in one day
4return Math.round(Math.abs((date2 - date1) / oneDay));
5}
6
7const startDate = new Date("October 1, 2023");
8const endDate = new Date("October 15, 2023");
9
10console.log(`Days between ${startDate.toDateString()} and ${endDate.toDateString()}:`, getDaysBetweenDates(startDate, endDate));
Output
Days between Sun Oct 01 2023 and Wed Oct 15 2023: 14

Summary

  • The Date object in JavaScript is used to work with dates and times.
  • You can create date instances using various methods, including the current date, a string, or a timestamp.
  • Retrieve individual components of a date using getter methods like getFullYear(), getMonth(), etc.
  • Set individual components using setter methods like setFullYear(), setMonth(), etc.
  • Format dates into strings using methods like toString(), toDateString(), toLocaleString(), and toISOString().

What's Next?

In the next tutorial, we'll explore JavaScript Symbols. Symbols are a unique primitive data type that can be used as keys for object properties. They provide a way to create unique identifiers, which is useful in various programming scenarios. Stay tuned!


PreviousJavaScript MapsNext JavaScript Symbols

Recommended Gear

JavaScript MapsJavaScript Symbols