Skip to main content

JavaScript Date Get Methods

By SamK
0
0 recommends
Category(s)
Topic(s)

In this tutorial, you'll learn about JavaScript date methods.

The Date() Constructor

In JavaScript, date objects are created using the new Date() constructor.

const currentDate = new Date();
console.log(currentDate); // Displays current date and time

JavaScript Date Get Methods

The get methods extract specific information from a date object.

getFullYear(): Returns the year as a four-digit number (yyyy).
getMonth(): Returns the month as a number (0-11).
getDate(): Returns the day of the month (1-31).
getDay(): Returns the weekday as a number (0-6).
getHours(): Returns the hour (0-23).
getMinutes(): Returns the minutes (0-59).
getSeconds(): Returns the seconds (0-59).
getMilliseconds(): Returns the milliseconds (0-999).
getTime(): Returns milliseconds since January 1, 1970.

The get methods return local time. Time in a date object is static and does not update dynamically.

The getFullYear() Method

Returns the full year as a four-digit number.

const date = new Date("2025-02-13");
console.log(date.getFullYear()); // Output: 2025

const currentDate = new Date();
console.log(currentDate.getFullYear()); // Output: 2025

The getMonth() Method

Returns the month as a number (0-11).

const date = new Date("2025-02-13");
console.log(date.getMonth()); // Output: 1 (February)

const currentDate = new Date();
console.log(currentDate.getMonth()); // Output: 2 (March)

Converting Month to Name

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const date = new Date("2025-02-13");
console.log(months[date.getMonth()]); // Output: February

The getDate() Method

Returns the day of the month (1-31).

const date = new Date("2025-02-13");
console.log(date.getDate()); // Output: 13

The getHours() Method

Returns the hour (0-23).

const date = new Date("2025-02-13 10:20:30");
console.log(date.getHours()); // Output: 10

The getMinutes() Method

Returns the minutes (0-59).

const date = new Date("2025-02-13 10:20:30");
console.log(date.getMinutes()); // Output: 20

The getSeconds() Method

Returns the seconds (0-59).

const date = new Date("2025-02-13 10:20:30");
console.log(date.getSeconds()); // Output: 30

The getMilliseconds() Method

Returns the milliseconds (0-999).

const date = new Date("2025-02-13");
console.log(date.getMilliseconds()); // Output: 0

The getDay() Method

Returns the day of the week (0-6).

const date = new Date("2025-02-13");
console.log(date.getDay()); // Output: 4 (Thursday)

Converting Day to Name

const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const date = new Date("2025-02-13");
console.log(days[date.getDay()]); // Output: Thursday

The getTime() Method

Returns milliseconds since January 1, 1970.

const date = new Date("2025-02-13");
console.log(date.getTime()); // Output: 1739404800000

The Date.now() Method

Returns milliseconds since January 1, 1970.

console.log(Date.now());

Convert Milliseconds to Years

const minute = 1000 * 60;      // 60,000 milliseconds in a minute
const hour = minute * 60;      // 3,600,000 milliseconds in an hour
const day = hour * 24;         // 86,400,000 milliseconds in a day
const year = day * 365;        // 31,536,000,000 milliseconds in a year

console.log(Math.round(Date.now() / year));

JavaScript UTC Date Get Methods

getUTCDate(), getDate(): Returns the UTC day of the month. 
getUTCFullYear(), getFullYear(): Returns the UTC year. 
getUTCMonth(), getMonth(): Returns the UTC month (0-11). 
getUTCDay(), getDay(): Returns the UTC weekday (0-6).
getUTCHours(), getHours(): Returns the UTC hour (0-23).  
getUTCMinutes(), getMinutes(): Returns the UTC minutes (0-59). 
getUTCSeconds(), getSeconds(): Returns the UTC seconds (0-59). 
getUTCMilliseconds(), getMilliseconds(): Returns the UTC milliseconds (0-999). 

  • UTC time is the same as GMT (Greenwich Mean Time).
  • The difference between local time and UTC time can be up to 24 hours.

The getTimezoneOffset() Method

Returns the time zone difference in minutes between local time and UTC time.

const date = new Date();
console.log(date.getTimezoneOffset()); // Output: -300 (Minutes)

Questions & Answers