JavaScript Date Methods

Date methods let you read and change the individual parts of a date, such as the year, month, day, and time.

Reading parts of a date

Once you have a Date object, a family of get methods lets you pull out each component. These methods return numbers based on the local time zone of the environment. Because the internal month value is zero-based, getMonth returns 0 for January through 11 for December, while getDate returns the day of the month from 1 to 31.

MethodReturnsRange
getFullYear()The four-digit yeare.g. 2026
getMonth()The month0 to 11
getDate()Day of the month1 to 31
getDay()Day of the week0 (Sun) to 6 (Sat)
getHours()The hour0 to 23
getMinutes()The minutes0 to 59

Reading date components

<!DOCTYPE html>
<html>
<body>

<h2>Reading Date Components</h2>

<script>
const d = new Date("2026-07-15T14:05:00");

console.log(d.getFullYear()); // 2026
console.log(d.getMonth());    // 6  (July)
console.log(d.getDate());     // 15
console.log(d.getDay());      // day of week, 0-6
console.log(d.getHours());    // 14
</script>

</body>
</html>

Changing parts of a date

Each get method has a matching set method that updates one component of the date in place. Setting a value that is out of the normal range causes the date to roll over sensibly. For example, setting the month to 12 moves the date into January of the next year. This makes it easy to do date arithmetic like adding days or months.

Updating and rolling over dates

<!DOCTYPE html>
<html>
<body>

<h2>Updating Dates</h2>

<script>
const d = new Date("2026-07-15");

d.setFullYear(2027);
console.log(d.getFullYear()); // 2027

// Add 20 days; the month rolls over automatically
d.setDate(d.getDate() + 20);
console.log(d.toDateString());
</script>

</body>
</html>

UTC methods

The standard get and set methods work in local time, which depends on the machine running the code. When you need results that are independent of time zone, use the UTC variants such as getUTCFullYear and getUTCHours. These read the date in Coordinated Universal Time, which is essential when your code runs on servers in different regions or shares dates between systems.

  • getHours() and getUTCHours() may differ depending on the time zone offset.
  • UTC methods give consistent results everywhere, useful for storage and comparison.
  • Date.now() returns the current time as milliseconds since the epoch, without creating an object.

Getting the current timestamp

The static method Date.now() returns the number of milliseconds between the Unix epoch and the current moment. It is a quick way to timestamp events or measure how long a piece of code takes to run, since you can subtract one timestamp from another.

Measuring elapsed time

<!DOCTYPE html>
<html>
<body>

<h2>Measuring Elapsed Time</h2>

<script>
const startTime = Date.now();

// simulate some work
let total = 0;
for (let i = 0; i < 1000000; i++) {
  total += i;
}

const elapsed = Date.now() - startTime;
console.log("Took " + elapsed + " ms");
</script>

</body>
</html>
Note: The get and set methods change or read a single field at a time. To avoid confusion when doing arithmetic across month and year boundaries, rely on the automatic roll-over behavior rather than calculating boundaries by hand.