Nearby lessons
40 of 40📅 JavaScript Date Methods
📌 What are JavaScript Date Methods?
The Date object in JavaScript is used to work with dates and times.
You can create a new date using new Date() and retrieve or set components using various methods.
🔸 Create Date Object
Use the new Date() constructor to create a Date object representing the current date and time.
Example02
🔸 Get Date Components (Get Methods)
Use the following methods to retrieve specific parts of a date:
getFullYear(): Get year as a 4-digit numbergetMonth(): Get month (0–11)getDate(): Get day of the month (1–31)getDay(): Get day of the week (0–6, Sunday–Saturday)getHours(): Get hour (0–23)getMinutes(): Get minutes (0–59)getSeconds(): Get seconds (0–59)getMilliseconds(): Get milliseconds (0–999)getTime(): Get time in milliseconds since Jan 1, 1970
Example03
🔸 Set Date Components (Set Methods)
You can modify parts of a Date object using the following setter methods:
setFullYear(year)setMonth(monthIndex)setDate(day)setHours(hour)setMinutes(min)setSeconds(sec)
Example04
🔸 Format Date as String
You can convert a date to a readable string using these methods:
toDateString(): Returns only the date portiontoTimeString(): Returns only the time portiontoISOString(): Returns in ISO format
Example05
🧠 Test Your Knowledge
10 QuestionsProgress: 0 / 10