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.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeCurrently Playing
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch