JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Get Date Methods

8 April 2025 | Category:

JavaScript provides several methods to extract date and time parts from a Date object. These are called “get” methods because they get information like the year, month, day, hours, and more.


âś… Create a Date Object

Before using get methods, you need a Date object:

const now = new Date();

đź“‹ List of Get Methods

MethodDescription
getFullYear()Gets the full year (e.g. 2025)
getMonth()Gets the month (0–11)
getDate()Gets the day of the month (1–31)
getDay()Gets the day of the week (0–6, 0 = Sunday)
getHours()Gets the hour (0–23)
getMinutes()Gets the minutes (0–59)
getSeconds()Gets the seconds (0–59)
getMilliseconds()Gets the milliseconds (0–999)
getTime()Gets time in milliseconds since 1970
getTimezoneOffset()Gets the difference from UTC in minutes

🔍 Examples

const now = new Date();

console.log(now.getFullYear());     // 2025
console.log(now.getMonth());        // 3 (April, since January = 0)
console.log(now.getDate());         // 8
console.log(now.getDay());          // 2 (Tuesday)
console.log(now.getHours());        // e.g., 14
console.log(now.getMinutes());      // e.g., 30
console.log(now.getSeconds());      // e.g., 15
console.log(now.getMilliseconds()); // e.g., 123
console.log(now.getTime());         // e.g., 1744103456789 (timestamp)
console.log(now.getTimezoneOffset()); // e.g., -330 (for IST)

đź§  Notes

  • getMonth() returns 0 for January and 11 for December.
  • getDay() returns 0 for Sunday and 6 for Saturday.
  • getTime() returns milliseconds since January 1, 1970 (UTC).
  • getTimezoneOffset() returns minutes behind UTC (useful for handling time zones).

đź§Ş Example Use Case

function getFormattedDate() {
  const d = new Date();
  const day = d.getDate();
  const month = d.getMonth() + 1; // Add 1 because months are 0-based
  const year = d.getFullYear();
  
  return `${day}/${month}/${year}`;
}

console.log(getFormattedDate()); // e.g., 8/4/2025

📌 Summary

Use these get methods to:

  • Display human-readable date and time.
  • Build custom date formats.
  • Work with real-time applications like clocks and calendars.