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
Loading Editor...
Live Preview

πŸ”Έ Get Date Components (Get Methods)

Use the following methods to retrieve specific parts of a date:

  • getFullYear(): Get year as a 4-digit number
  • getMonth(): 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
Loading Editor...
Live Preview

πŸ”Έ 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
Loading Editor...
Live Preview

πŸ”Έ Format Date as String

You can convert a date to a readable string using these methods:

  • toDateString(): Returns only the date portion
  • toTimeString(): Returns only the time portion
  • toISOString(): Returns in ISO format
Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

10 Questions

Progress: 0 / 10
Keep Going!Course Complete!