Open In App

JavaScript Date

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Date object is used to represent a moment in time. This time value is since 1 January 1970 UTC (Coordinated Universal Time).

We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax.

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);
  • value: This value is the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • dateString: This represents a date format.
  • year: This is represented by integer values ranging from the years 1900 to 1999.
  • month: This is represented by integer values ranging from 0 for January to 11 for December.
  • day: This is an optional parameter. This is represented by an integer value for the day of the month.
  • hours: This is optional. This is represented by an integer value for the hour of the day.
  • minutes: This is optional. This is represented by an integer value for the minute of a time.
  • seconds: This is optional. This is represented by an integer value for the second of a time.
  • milliseconds: This is optional. This is represented by an integer value for the millisecond of a 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

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads