Mastering Dates and Date Methods in JavaScript ⏳ | JavaScript Journey | In Hindi | Death Code - DeathCode

JavaScript Date Object Documentation

The Date object in JavaScript is used to work with dates and times. It provides various methods to manipulate and format date and time values.

Creating Date Objects

You can create a new date object using the new Date() constructor. If called without arguments, it creates a date object representing the current date and time.

Example Code

let date = new Date(); // Creates a new date object for the current date and time
 
console.log(date.toString()); // Outputs: Full date and time string
console.log(date.toDateString()); // Outputs: Date portion as a string
console.log(date.toTimeString()); // Outputs: Time portion as a string
console.log(date.toLocaleString()); // Outputs: Date and time in local format
console.log(date.toISOString()); // Outputs: Date in ISO format
console.log(date.getDate()); // Outputs: Day of the month (1-31)
console.log(date.getDay()); // Outputs: Day of the week (0-6, where 0 is Sunday)
console.log(date.getTime()); // Outputs: Number of milliseconds since January 1, 1970 

Explanation

  • toString() returns a string representing the date and time in a human-readable format.
  • toDateString() returns the date portion of the date object as a string.
  • toTimeString() returns the time portion of the date object as a string.
  • toLocaleString() returns the date and time in a format that is appropriate for the user's locale.
  • toISOString() returns the date in ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ).
  • getDate() returns the day of the month (1-31).
  • getDay() returns the day of the week (0-6), where 0 represents Sunday.
  • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Working with Specific Dates

You can also create date objects for specific dates by passing parameters to the Date constructor.

Example Code

// Creating specific date objects
let anotherDate = new Date(2023, 11, 12); // December 12, 2023
// let anotherDate = new Date(2023, 9, 22); // October 22, 2023
console.log(anotherDate.toLocaleString()); // Outputs: Local date and time format
 
let moreDate = new Date(2024, 9, 21); // October 21, 2024
const seconds = (moreDate.getTime() - anotherDate.getTime()) / 1000; // Difference in seconds
console.log(seconds / 60 / 60 / 24); // Outputs: Difference in days

Explanation

  • When creating a date object with new Date(year, month, day), the month is zero-indexed (0 for January, 1 for February, etc.).
  • The difference between two date objects can be calculated using getTime(), which returns the time in milliseconds. Dividing by 1000 converts it to seconds, and further division by 60 and 60 and 24 converts it to days.

Getting the Current Timestamp

You can also get the current timestamp in milliseconds since January 1, 1970, using the Date.now() method.

Example Code

let newDate = Date.now(); // Gets the current timestamp in milliseconds

Explanation

  • Date.now() returns the number of milliseconds elapsed since the Unix epoch (January 1, 1970).

Conclusion

The Date object is essential for handling dates and times in JavaScript. Understanding how to create and manipulate date objects is crucial for effective date management in applications.

Β© 2024 DeathCode. All Rights Reserved.