DEV Community

Cover image for Date Methods JavaScript
Vibhakar Kumar
Vibhakar Kumar

Posted on

Date Methods JavaScript

Date methods in JavaScript enable manipulation and formatting of dates.
The most common methods include getDate() for retrieving the day of the month, getMonth() for getting the month (0-11), getFullYear() for obtaining the year, and getDay() for fetching the day of the week (0-6). Additionally, there are setters like setDate(), setMonth(), and setFullYear() for modifying date components. toLocaleDateString() and toLocaleTimeString() convert dates and times to locale-specific strings. getTime() returns the number of milliseconds since January 1, 1970, while Date.now() directly gives the current timestamp. These methods facilitate the comprehensive handling of dates in JavaScript applications.

Example are :

const date = new Date(); //Tue Feb 20 2024 11:31:00 GMT+0530

date.getDate(); // month's date: 20
date.getMonth(); // Month with 0 index: 1
date.getFullYear(); // Year: 2024
date.getHours(); // Hours: 11
date.getMinutes(); // Minutes: 54
date.getSeconds(); // Seconds: 34
date.getMilliseconds(); // Millisecond: 186
date.getTime(); // Time: 1708409236568
date.setDate(23); // Set date: 23
date.setMonth(3); // Set month: 3
date.setFullYear(2024); // Set year: 2024
date.setHours(10); // Set hours: 10
date.setMinutes(45); // Set minutes: 45
date.setSeconds(49); // Set seconds: 49
date.setMilliseconds(176); // Set Milliseconds: 176 date.setTime(1648101466176); // Set time: 1648101466176


Thank You !
Vibhakar Kumar

Top comments (0)