JavaScript Date Objects
The JavaScript Date object is designed to work with dates and times, providing a comprehensive set of methods for creating, manipulating, and formatting date and time values. It represents a point in time, with precision down to milliseconds, and can be utilized to perform various operations on dates.
Creating Date Objects:
To create a new Date object, you can use the new Date() constructor. If no arguments are provided, it creates a Date object representing the current date and time. Alternatively, you can pass specific values for the year, month, day, hour, minute, second, and millisecond.
// Creating a Date object with the current date and time
let currentDate = new Date();
// Creating a Date object with specific values
let customDate = new Date(2024, 0, 9, 12, 30, 0, 0); // Year, Month (0-indexed), Day, Hour, Minute, Second, Millisecond
In the below PDF we discuss about Javascript Date Objects in detail in simple language, Hope this will help in better understanding.
Common Date Object Methods:
- getMonth(): Returns the month (0-indexed) of the Date object.
- getDate(): Returns the day of the month (1-31) of the Date object.
- getFullYear(): Returns the year of the Date object.
getHours(), getMinutes(), - getSeconds(): Return the hour, minute, and second of the Date object, respectively.
- getTime(): Returns the timestamp (milliseconds since January 1, 1970) of the Date object.
toDateString(), toTimeString(), - toLocaleDateString(): Convert the Date object to human-readable date and time strings.
For Examples: let month = customDate.getMonth(); // Returns 0 for January
let day = customDate.getDate();
let year = customDate.getFullYear();
let hours = customDate.getHours();
let minutes = customDate.getMinutes();
let seconds = customDate.getSeconds();
let timestamp = customDate.getTime();
Manipulating Dates:
The Date object allows you to easily manipulate dates using various methods. For example, you can add or subtract days, months, or years.
customDate.setDate(customDate.getDate() + 7); // Add 7 days
customDate.setMonth(customDate.getMonth() + 1); // Add 1 month
customDate.setFullYear(customDate.getFullYear() - 1); // Subtract 1 year
You can iterate through the Set elements using the for…of loop or forEach() method. The elements are accessed in the insertion order. For example,
const set = new Set([1, 2, 3]);
// looping through Set
for (let i of set) {
console.log(i);
}
Output:
1
2
3
Related Question
JavaScript doesn’t provide a built-in method for formatting dates, but you can create a custom function or use libraries like Intl.DateTimeFormat to format dates according to your requirements.
You can compare two dates using comparison operators (<, <=, >, >=) or by converting them to timestamps using the getTime() method and then comparing the timestamps.
The getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. This value is useful for comparing and calculating time differences between dates.
You can calculate the difference between two dates by subtracting one date’s timestamp from another. This can be done using the getTime() method, and then converting the difference into the desired units (e.g., days, hours, minutes).
Relevant
JavaScript JSON JSON stands for
JavaScript Function Generator Function generators,
JavaScript WeakSet A WeakSet is
JavaScript WeakMap A WeakMap is
JavaScript Proxy A JavaScript Proxy
JavaScript Boolean JavaScript Boolean is
JavaScript BigInt JavaScript BigInt represented