JavaScript Date Methods

JavaScript Date methods are used to read, change, compare, and format values stored in a Date object. A JavaScript Date object represents a specific moment in time, internally stored as milliseconds since 1 January 1970 UTC.

This tutorial explains the commonly used JavaScript Date get methods, set methods, UTC methods, timestamp methods, and formatting methods with examples. It also points out common mistakes such as zero-based months, getDay() behavior, and invalid date parsing.

Creating a JavaScript Date object before using Date methods

Most JavaScript Date methods are called on a Date object. You can create a date for the current time, from a timestamp, from date parts, or from a supported date string.

</>
Copy
const now = new Date();
const fromMilliseconds = new Date(1704067200000);
const fromParts = new Date(2024, 0, 1, 10, 30, 0);
const fromIsoString = new Date("2024-01-01T10:30:00Z");

In new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds), the month is zero-based. January is 0, February is 1, and December is 11.

JavaScript Date get methods for reading date and time values

Get methods read individual parts of a JavaScript Date object. The non-UTC get methods return values according to the user’s local time zone.

JavaScript Date methodReturnsRange or note
getDate()Day of the month1 to 31
getDay()Day of the week0 for Sunday to 6 for Saturday
getFullYear()YearFour-digit year such as 2024
getHours()Hour0 to 23
getMilliseconds()Milliseconds0 to 999
getMinutes()Minutes0 to 59
getMonth()Month index0 for January to 11 for December
getSeconds()Seconds0 to 59
getTime()Timestamp in millisecondsMilliseconds since 1 January 1970 UTC

Two methods are often confused: getDate() returns the day of the month, while getDay() returns the day of the week.

Example – Date get methods

The following example demonstrates the Date() get methods.

index.html

</>
Copy
<!doctype html>
<html>
<body>
    <h1>JavaScript Date Methods Example</h1>
    <p id="message"></p>
    
    <script>
        <!-- your JavaScript goes here -->
        var date = new Date();
        
        var msg = "";
        
        // Date
        msg += "Date : ";
        msg += date; msg +="<br><hr>";
        
        msg += "getDate() : ";
        msg += date.getDate(); msg +="<br>";
        
        msg += "getDay() : ";
        msg += date.getDay(); msg +="<br>";
        
        msg += "getFullYear() : ";
        msg += date.getFullYear(); msg +="<br>";
        
        msg += "getHours() : ";
        msg += date.getHours(); msg +="<br>";
        
        msg += "getMilliseconds() : ";
        msg += date.getMilliseconds(); msg +="<br>";
        
        msg += "getMinutes() : ";
        msg += date.getMinutes(); msg +="<br>";
        
        msg += "getMonth() : ";
        msg += date.getMonth(); msg +="<br>";
        
        msg += "getSeconds() : ";
        msg += date.getSeconds(); msg +="<br>";
        
        msg += "getTime() : ";
        msg += date.getTime(); msg +="<br>";
        
        document.getElementById("message").innerHTML = msg;
    </script>
    
</body>
</html>

JavaScript Date set methods for changing date and time values

Set methods change parts of an existing Date object. They update the object in place and return the new timestamp value in milliseconds. JavaScript does not have a setDay() method. To change the day of the month, use setDate().

JavaScript Date methodSetsRange or note
setDate()Day of the month1 to 31; adjusts month if the value overflows
setFullYear()YearCan also accept month and date
setHours()Hour0 to 23; can also accept minutes, seconds, milliseconds
setMilliseconds()Milliseconds0 to 999
setMinutes()Minutes0 to 59; can also accept seconds, milliseconds
setMonth()Month index0 for January to 11 for December
setSeconds()Seconds0 to 59; can also accept milliseconds
setTime()Whole date-time valueTimestamp in milliseconds since 1 January 1970 UTC

Example – Date set methods

The following example demonstrates the Date() set methods.

index.html

</>
Copy
<!doctype html>
<html>
<body>
    <h1>JavaScript Date Methods Example</h1>
    <p id="message"></p>
    
    <script>
        <!-- your JavaScript goes here -->
        var msg = "";
        
        var date = new Date();
        
        msg += "Date before set methods : ";
        msg += date;
        msg += "<br>";
        
        date.setDate(24);
        date.setFullYear(2020);
        date.setHours(14);
        date.setMilliseconds(556);
        date.setMinutes(41);
        date.setMonth(8);
        date.setSeconds(22);
        
        msg += "Date after set methods : ";
        msg += date;
        msg += "<br>";
        
        date.setTime(1516993680832);
        
        msg += "Date after setTime() : ";
        msg += date;
        msg += "<br>";
        
        document.getElementById("message").innerHTML = msg;
    </script>
	
</body>
</html>

The exact output of date examples can vary by time zone because methods such as getHours(), setHours(), and the default string form of a Date use the local time zone of the environment where the code runs.

JavaScript UTC Date methods for time-zone independent values

JavaScript provides UTC versions of many Date methods. Use these when you want to read or set values in Coordinated Universal Time instead of local time.

Local Date methodUTC Date methodPurpose
getFullYear()getUTCFullYear()Read the year
getMonth()getUTCMonth()Read the zero-based month index
getDate()getUTCDate()Read the day of the month
getHours()getUTCHours()Read the hour
setFullYear()setUTCFullYear()Set the year
setMonth()setUTCMonth()Set the zero-based month index
setDate()setUTCDate()Set the day of the month
setHours()setUTCHours()Set the hour
</>
Copy
const date = new Date("2024-01-01T00:00:00Z");

console.log(date.getFullYear());     // local year
console.log(date.getUTCFullYear());  // UTC year
console.log(date.getHours());        // local hour
console.log(date.getUTCHours());     // UTC hour

JavaScript Date formatting methods: toString(), toISOString(), and toLocaleString()

Date formatting methods convert a Date object into a readable string. Different methods are useful for different situations.

Date formatting methodUse it forExample behavior
toString()Readable local date and timeIncludes local time zone information
toDateString()Date-only displayReturns a readable date without the time
toTimeString()Time-only displayReturns time and time zone information
toISOString()Reliable ISO 8601 storage or API formatReturns UTC date-time ending with Z
toLocaleDateString()Localized date displayDepends on locale and formatting options
toLocaleTimeString()Localized time displayDepends on locale and formatting options
toLocaleString()Localized date and time displayCan be customized with locale options

For data exchange and storage, prefer an ISO format such as YYYY-MM-DDTHH:mm:ss.sssZ. For user-facing display, prefer toLocaleDateString(), toLocaleTimeString(), or Intl.DateTimeFormat.

</>
Copy
const date = new Date("2024-01-15T09:30:00Z");

console.log(date.toISOString());
console.log(date.toLocaleDateString("en-IN"));
console.log(date.toLocaleString("en-IN", {
  dateStyle: "medium",
  timeStyle: "short",
  timeZone: "Asia/Kolkata"
}));

Getting the current date in JavaScript with Date methods

To build a simple current date string, use getFullYear(), getMonth(), and getDate(). Add 1 to the month returned by getMonth() because it starts from 0.

</>
Copy
const today = new Date();

const year = today.getFullYear();
const month = today.getMonth() + 1;
const day = today.getDate();

const formattedDate = `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;

console.log(formattedDate);

This pattern gives a local date in YYYY-MM-DD style. If you need a UTC date, use getUTCFullYear(), getUTCMonth(), and getUTCDate() instead.

Comparing dates in JavaScript using getTime()

The getTime() method returns a number, so it is useful for comparing two dates or finding the difference between them.

</>
Copy
const startDate = new Date("2024-01-01T00:00:00Z");
const endDate = new Date("2024-01-05T00:00:00Z");

const differenceInMilliseconds = endDate.getTime() - startDate.getTime();
const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);

console.log(differenceInDays);
4

You can also use Date.now() when you only need the current timestamp and do not need to create a full Date object.

</>
Copy
const currentTimestamp = Date.now();
console.log(currentTimestamp);

Handling invalid JavaScript Date values safely

A date can become invalid if the input string is not recognized or if the value cannot be converted to a valid timestamp. Use Number.isNaN(date.getTime()) to check whether a Date object is invalid.

</>
Copy
const date = new Date("not a date");

if (Number.isNaN(date.getTime())) {
  console.log("Invalid date");
} else {
  console.log(date.toISOString());
}

For predictable parsing, avoid ambiguous formats such as 01/02/2024 because they may be interpreted differently depending on environment and locale. Use ISO-style strings when possible.

Common mistakes with JavaScript Date methods

  • Using getMonth() directly for display: add 1 before displaying the month to users.
  • Using getDay() for the day of the month: use getDate() for the day of the month. getDay() gives the weekday number.
  • Expecting setDate() to reject overflow values: JavaScript adjusts the month automatically when the day value overflows.
  • Forgetting that local methods depend on time zone: use UTC methods or ISO strings when consistent cross-time-zone behavior is needed.
  • Calling a non-existing setDay() method: JavaScript Date has getDay(), but no setDay().

JavaScript Date methods quick reference for local and UTC values

TaskLocal Date methodUTC Date method
Read yeargetFullYear()getUTCFullYear()
Read monthgetMonth()getUTCMonth()
Read day of monthgetDate()getUTCDate()
Read weekdaygetDay()getUTCDay()
Read hourgetHours()getUTCHours()
Set yearsetFullYear()setUTCFullYear()
Set monthsetMonth()setUTCMonth()
Set day of monthsetDate()setUTCDate()
Set hoursetHours()setUTCHours()

For the full list of Date methods and exact method signatures, see the MDN JavaScript Date reference.

FAQs on JavaScript Date methods

How do you use Date methods in JavaScript?

Create a Date object with new Date(), and then call methods such as getFullYear(), getMonth(), getDate(), setFullYear(), or toISOString() on that object. For example, new Date().getFullYear() returns the current year according to the local time zone.

What is the best date format for JavaScript Date parsing?

For predictable date-time values, use ISO 8601 style strings such as 2024-01-15T09:30:00Z. Avoid ambiguous short formats such as 01/02/2024 when parsing dates because they may be interpreted differently across environments.

What is the difference between getDate() and getDay() in JavaScript?

getDate() returns the day of the month from 1 to 31. getDay() returns the day of the week from 0 to 6, where 0 means Sunday.

Why does JavaScript getMonth() return 0 for January?

getMonth() returns a zero-based month index. January is 0, February is 1, and December is 11. Add 1 when you need to display the month number to users.

Does JavaScript Date have a setDay() method?

No. JavaScript Date has getDay() to read the weekday, but it does not have setDay(). To change the day of the month, use setDate().

Editorial QA checklist for JavaScript Date methods examples

  • Confirm that getDate() is described as day of the month and getDay() as day of the week.
  • Confirm that examples mention zero-based month values for getMonth(), setMonth(), and the Date constructor.
  • Use language-javascript for JavaScript examples and output for result-only code blocks.
  • State whether an example depends on local time zone or UTC time.
  • Prefer ISO date strings in parsing examples to avoid ambiguous browser behavior.

Summary of JavaScript Date methods

JavaScript Date methods help you read, update, compare, and format date-time values. Use get methods such as getFullYear() and getDate() to read values, set methods such as setFullYear() and setDate() to update values, getTime() or Date.now() for timestamps, and formatting methods such as toISOString() or toLocaleString() for string output.

In this JavaScript Tutorial, we learned about different methods of Date Object, with examples.