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
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔸 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
Example03
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔸 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
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🔸 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
Example05
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10