Flat Preloader Icon

JS Date

The Date object in JavaScript is used for working with dates and times. It allows you to create, manipulate, and format dates and times.

1. Creating Date Objects:

You can create a Date object using the new Date() constructor. You can pass various parameters to create dates representing different times:

  • No parameters: Creates a Date object representing the current date and time.
  • Milliseconds since January 1, 1970: Creates a Date object for a specific date and time.Date string: Creates a Date object from a string representation of a date.
  • Date string: Creates a Date object from a string representation of a date.
					const currentDate = new Date(); 
// Current date and time
const specificDate = new Date(2022, 0, 1); 
// January 1, 2022
const dateString = new Date("2022-01-01");  
//Same as above


2. Getting Date and Time Components:

  • You can get various components of a Date object such as the year, month, day, hour, minute, second, and milliseconds using various methods like getFullYear(), getMonth(), getDate(), etc.
					const year = currentDate.getFullYear();
const month = currentDate.getMonth(); 
// Month is zero-based (0-11)
const day = currentDate.getDate();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
const millisecond = currentDate.getMilliseconds();


3. Setting Date and Time Components:

  • You can set various components of a Date object using methods like setFullYear(), setMonth(), setDate(), etc.
// Month is zero-based (0-11)


4. Formatting Dates:

You can format Date objects into strings using various methods like toDateString(), toLocaleDateString(), toTimeString(), toLocaleTimeString(), toISOString(), toUTCString(), etc.

// Outputs: "Thu Jul 15 2023"
// Outputs: "12:30:00 PM"
// Outputs: "2023-07-15T12:30:00.000Z"


5. Date Arithmetic:

  • You can perform arithmetic operations on Date objects to add or subtract days, hours, minutes, etc.
					const futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7); 
// Add 7 days
// Outputs: Date 7 days from currentDate



Here is a description of the parameters −

  • No Argument − With no arguments, the Date() constructor creates a Date object set to the current date and time.

  • milliseconds − When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates a date that represents five seconds past midnight on 1/1/70.

  • datestring − When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse() method.

  • 7 agruments − To use the last form of the constructor shown above. Here is a description of each argument −

    • year − Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.

    • month − Integer value representing the month, beginning with 0 for January to 11 for December.

    • date − Integer value representing the day of the month.

    • hour − Integer value representing the hour of the day (24-hour scale).

    • minute − Integer value representing the minute segment of a time reading.

    • second − Integer value representing the second segment of a time reading.

    • millisecond − Integer value representing the millisecond segment of a time reading.