JavaScript Date

The JavaScript Date object serves as a fundamental component for managing date and time values within applications. It encapsulates a moment in time, allowing developers to perform various operations such as date arithmetic, formatting, and manipulation.

The time value represented by a Date object is measured in milliseconds since the Unix Epoch, which is defined as January 1, 1970, at 00:00:00 UTC (Coordinated Universal Time). This epoch serves as a reference point for calculating time intervals and representing dates across different time zones.

Creating a Date object involves invoking the new Date() constructor, which initializes the object with the current date and time based on the system’s local time zone. Additionally, the Date constructor supports various parameter options to specify a specific date and time, including year, month, day, hour, minute, second, and milliseconds.

Creating a Date Object:

You can create a Date object in several ways:

JavaScript Date Syntax: 

new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
FieldDescription
valueThe number of milliseconds since January 1, 1970, 00:00:00 UTC.
dateStringRepresents a date format.
yearAn integer representing the year, ranging from 1900 to 1999.
monthAn integer representing the month, ranging from 0 for January to 11 for December.
dayAn optional integer representing the day of the month.
hoursAn optional integer representing the hour of the day.
minutesAn optional integer representing the minute of the time.
secondsAn optional integer representing the second of the time.
millisecondsAn optional integer representing the millisecond of the time.

Return Values:

It returns the present date and time if nothing as the parameter is given otherwise it returns the date format and time in which the parameter is given.

Getting Date Components:

You can get various components of a date (such as year, month, day, hour, minute, second, etc.) using methods provided by the Date object:

Example: The code initializes a `Date` object representing the current date and time. It then retrieves various components such as year, month (zero-based), day of the month, hours, minutes, and seconds from this object. These components are stored in separate variables for further use or display.

Javascript
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth(); // Note: Month is zero-based (0 for January, 11 for December)
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();

Formatting Dates:

Formatting dates in JavaScript can be done manually, or by using libraries like moment.js. However, with modern JavaScript, you can also achieve formatting using Intl.DateTimeFormat:

Example: The code initializes a `Date` object representing the current date. It then formats this date using the `Intl.DateTimeFormat` constructor with the locale set to ‘en-US’, displaying it in the format ‘month/day/year’.

Javascript
let date = new Date();
let formattedDate = new Intl.DateTimeFormat('en-US').format(date);
console.log(formattedDate); // Output: "2/23/2024" (assuming today's date is Feb 23, 2024)

Output
2/23/2024

Manipulating Dates:

You can manipulate dates using various methods provided by the Date object.

Example: The code initializes a `Date` object representing the current date. It then increments the date by 7 days using `setDate()`. Finally, it logs the modified date to the console.

Javascript
let date = new Date();
date.setDate(date.getDate() + 7); // Adds 7 days to the current date
console.log(date);

Output
2024-03-01T06:14:35.429Z

Example: The code initializes a `Date` object with the provided parameters: year (1996), month (10 for November), day (13), hours (5), minutes (30), seconds (22), and milliseconds (0 by default). It then logs this date to the console.

javascript
// When some numbers are taken as the parameter 
// then they are considered as year, month, day, 
// hours, minutes, seconds, milliseconds 
// respectively.
let A = new Date(1996, 10, 13, 5, 30, 22);

console.log(A);

Output
1996-11-13T05:30:22.000Z

Furthermore, the Date object provides a range of methods for retrieving and manipulating date and time components, such as getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds(), and getMilliseconds().

By leveraging the JavaScript Date object, developers can effectively manage temporal data within their applications, enabling tasks such as scheduling events, calculating time differences, and formatting dates for display.

In summary, the Date object serves as a versatile tool for handling date and time values in JavaScript, offering a comprehensive suite of functionalities to meet the diverse needs of web development projects.

We have a complete list of Javascript Date object methods, to check those please go through this JavaScript Date Object Complete Reference article.

Supported Browsers: The browsers supported by JavaScript Date are listed below: 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above


Contact Us