A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-date/ below:

JavaScript Date - GeeksforGeeks

JavaScript Date

Last Updated : 11 Jul, 2025

The JavaScript Date object represents a specific moment in time, measured in milliseconds since the Unix Epoch (January 1, 1970). It's crucial for working with date and time in web applications, providing methods for tasks like date manipulation, formatting, and calculations.

What is the JavaScript Date Object?

In JavaScript, the Date object tracks time as the number of milliseconds since the Unix Epoch. You can create a Date object using the new Date() constructor, which allows you to specify either the current date and time or a particular date.

Creating a Date Object

The Date object can be initialized in various ways using the new Date() constructor. It supports multiple formats for defining dates:

Syntax
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
Different Ways to Create a Date Object

1. Current Date and Time

const currentDate = new Date();

2. Milliseconds since Epoch

const dateFromMilliseconds = new Date(1609459200000);  // Represents January 1, 2021

3. Date String

const dateFromString = new Date("March 25, 2024");

4. Specific Date and Time

const specificDate = new Date(2023, 10, 13, 5, 30, 22);  // Represents November 13, 2023, 5:30:22 AM
Parameters for the Date Constructor

The Date constructor allows for different parameter types to define a specific date and time. Here’s a breakdown of the possible parameters:

Field Description value The number of milliseconds since January 1, 1970, 00:00:00 UTC. dateString Represents a date format. year An integer representing the year, ranging from 1900 to 1999. month An integer representing the month, ranging from 0 for January to 11 for December. day An optional integer representing the day of the month. hours An optional integer representing the hour of the day. minutes An optional integer representing the minute of the time. seconds An optional integer representing the second of the time. milliseconds An optional integer representing the millisecond of the time. Retrieving Date Components

You can retrieve various parts of a date using the following methods:

Formatting Dates

You can format dates manually or use built-in options such as Intl.DateTimeFormat to display the date in a more readable format.

JavaScript
const date = new Date();
const formattedDate = new Intl.DateTimeFormat('en-US').format(date);  // Displays as month/day/year
console.log(formattedDate);
Manipulating Dates

You can modify dates using various methods provided by the Date object, such as:

Example 1: Adding 7 Days

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-07-15T10:25:17.602Z
Example 2: Creating a Specific Date

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: 

Note: Ensure your browser is updated to the latest version for full compatibility and optimal performance when working with JavaScript Date functionalities..


Javascript program to convert Date to Number


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4