HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Date Get Methods

Date get methods allow you to extract specific information from a Date object (like the year, month, or day). These methods read the information but never change the original object.


Essential Get Methods

Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month (0-11)
getDate() Get the day as a number (1-31)
getDay() Get the weekday as a number (0-6)
getTime() Get the time (milliseconds since Jan 1, 1970)

Mapping Numbers to Names

Because getMonth() and getDay() return numbers, you often need an array to display them in a human-readable format.

const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = new Date();
let dayName = days[d.getDay()]; // Returns name based on index

The getTime() Method

The getTime() method returns the number of milliseconds since January 1, 1970. This value is extremely useful for performing math calculations between two dates.

const d = new Date();
console.log(d.getTime()); // Returns a long number like 1648181234567
Tip: Always use getFullYear(). The older getYear() method is deprecated and may return unpredictable results in different browsers.

Key Points to Remember

  • getFullYear() is the modern standard for year extraction
  • Months are 0-indexed (0 = January)
  • Days are 0-indexed (0 = Sunday, 6 = Saturday)
  • getDate() gives the day of the month (1-31)
  • getTime() is the primary tool for date arithmetic
  • All get methods return values based on the local time of the browser