Date object in js
The Date object in JavaScript is used for working with dates and times. The Date object represents a specific moment in time, including the year, month, day, hour, minute, second, and milliseconds. The date object in JavaScript provides various methods and properties for working with dates and times, allowing for easy manipulation and management of date-related information in applications.
Date object creation
Without parameters
let currentDate = new Date(); // Creates a Date object representing the current date and time.
With parameters
let specificDate = new Date('2023-10-31'); // Creates a Date object for a specific date.
Properties and Methods of the Date Object
Date.toString() method: The toString() method converts the date object to a string in a specific format, showing the date and time based on the current locale (e.g., including the day of the week, month, day, year, and time in the local time zone) as shown in the example below.
let myDate = new Date() console.log(myDate.toString()); // Sun Nov 05 2023 22:07:41 GMT+0530 (India Standard Time)
Date.toISOString() method: The output of toISOString() will be a string representing the date and time in the following format: "YYYY-MM-DDTHH:mm:ss.sssZ" where YYYY represents year MM represents month DD represents day T separates the day from time HH represents the hour mm represents the minutes ss represents the seconds sss represents the milli seconds and Z represents the timezone as shown in the example below.
let myDate = new Date() console.log(myDate.toISOString()); // 2023-11-05T16:38:09.701Z
Date.toLocaleDateString() method: The Date.toLocaleDateString() method in JavaScript's Date object is used to convert a date to a string representing only the date portion according to the local system settings and locale-specific format. The example of this method is shown below
let myDate = new Date() console.log(myDate.toLocaleDateString()); // 5/11/2023 let myCreatedData3 = new Date("2023-01-14"); console.log(myCreatedData3.toLocaleDateString()); // 14/1/202
Date.toLocaleString() method: The Date.toLocaleString() method in JavaScript's Date object is used to convert a date to a string representing the date and time based on the local system settings and the locale-specific format. The example of this method is shown below
```javascript let myDate = new Date() console.log(myDate.toLocaleString()); // 5/11/2023, 10:08:53 pm
let myCreatedData2 = new Date(2023, 0, 23, 5, 3) console.log(myCreatedData2.toLocaleString()); // 23/1/2023, 5:03:00 am
5. Date.toLocaleTimeString() method: The Date.toLocaleTimeString() method retrieves and returns a string representation of the time without the date component, formatted according to the local system's settings and the user's locale
```javascript
let myDate = new Date()
console.log(myDate.toLocaleTimeString()); // 10:09:15 pm
Date.toJSON() method: The toJSON() method returns a string in the following format: "YYYY-MM-DDTHH:mm:ss.sssZ". This format represents the date and time in a standardized manner compatible with JSON (JavaScript Object Notation). Below is an example of the same.
let myDate = new Date() console.log(myDate.toJSON()); // 2023-11-05T16:39:35.302Z
Date.toDateString() method: The toDateString() method in JavaScript's Date object is used to obtain a string representation of the date portion (excluding the time) in a specific format based on the local system settings and locale. Below is an example of the same.
let myDate = new Date() console.log(myDate.toDateString()); //Sun Nov 05 2023 let myCreatedData = new Date(2023, 0, 23) console.log(myCreatedData.toDateString()); // Mon Jan 23 2023
Date.getDate() and Date.setDate() methods: The js getDay() method retrieves the day of the month (from 1 to 31) for a specified date. Date.setDate() method sets the day of the month for a specified date.
let date = new Date('2023-10-15'); let dayOfMonth = date.getDate(); // Retrieves the day of the month console.log(dayOfMonth); // Output: 15 let date = new Date('2023-10-15'); date.setDate(10); // Sets the day of the month to the 10th console.log(date); // Output: Sun Oct 10 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
Date.getDay() method: The getDay() method in JavaScript's Date object is used to retrieve the day of the week as a numeric value, where 0 represents Sunday, 1 represents Monday, and so on, up to 6 for Saturday.
let date = new Date('2023/11/05'); console.log(date.getDay()); //Output: 0 (Sunday)
Date.getMonth() and Date.setMonth() methods: The get method retrieves the month of a Date object, starting from 0 for January and ending with 11 for December, and the setMonth() method is used to set the month of a Date object. The example is shown below.
let date = new Date(); let month = date.getMonth(); console.log(month); // Output: 10(November as it starts from 0 0:Jan 11:Dec) date.setMonth(5); // Sets the month to June (remember, it's zero-based) console.log(date);//Output: 2023-06-05T16:13:51.004Z console.log(date.getMonth()) //Output: 5(June)
Date.getFullYear() and Date.setFullYear() methods: The getFullYear() method retrieves the year from a Date object and returns a four-digit representation, and the setFullYear() method is used to set the custom year for a date object.
let date = new Date(); console.log(date.getFullYear()); // Output: 2023 date.setFullYear(2022); // Sets the year to 2022 console.log(date);//Output: 2022-11-05T16:18:02.101Z console.log(date.getFullYear()) //Output: 2022
Date.getHours() and Date.setHours() methods: The getHours() method retrieves the hour of a Date object in local time, ranging from 0 to 23, and the setHours() method is used to set the hour of the Date object. Below is the example for the same.
let date = new Date(); console.log(date.getHours()); // Output: 21 [current Hour] date.setHours(23); // Sets the hour to 23 console.log(date);//Output: 2023-11-05T18:21:16.880Z console.log(date.getHours()) //Output: 23
Date.getMinutes() and Date.setMinutes() methods: These methods allow you to extract the minutes (using getMinutes()) or to set a specific minute (using setMinutes()) in a Date object. The getMinutes() method returns the minute part of the date, while using the setMinutes() you can modify the minutes to the desired value.
let date = new Date(); console.log(date.getMinutes()); // Output: 54 [current minutes] date.setMinutes(45); // Sets the minutes to 45 console.log(date);//Output: 2023-11-05T16:15:19.190Z console.log(date.getMinutes()) //Output: 45
Date.getSeconds() and Date.setSeconds() methods: These methods allow you to extract the seconds (using getSeconds()) or to set a specific number of seconds (using setSeconds()) in a Date object. The getSeconds() method returns the seconds part of the date, while using setSeconds() allows you to modify the seconds to the desired value.
let date = new Date(); console.log(date.getSeconds()); // Output: 48 [current seconds] date.setSeconds(10); // Sets the seconds to 10 console.log(date);//Output: 2023-11-05T16:28:10.375Z console.log(date.getSeconds()) //Output: 10
Date.getMilliseconds() and Date.setMilliseconds() methods: These methods allow you to extract the milliseconds (using getMilliseconds()) or to set a specific number of milliseconds (using setMilliseconds()) in a Date object. The getMilliseconds() method returns the milliseconds part of the date while using setMilliseconds() enables you to modify the milliseconds to the desired value.
let date = new Date(); console.log(date.getMilliseconds()); // Output: 230 [current seconds] date.setMilliseconds(100); // Sets the milliseconds to 100 console.log(date);//Output: 2023-11-05T16:30:30.100Z console.log(date.getMilliseconds()) //Output: 100