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.
Code Example
PREVIEW READY
πΈ 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
Code Example
PREVIEW READY
πΈ 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)
Code Example
PREVIEW READY
πΈ 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
Code Example
PREVIEW READY
π§ Test Your Knowledge
10 QuestionsProgress: 0 / 10
Keep Going!Course Complete!