JavaScript Date
JavaScript provides the built-in Date object to create, read, compare, and format dates and times. A JavaScript Date stores a time value internally as milliseconds from 1 January 1970, 00:00:00 UTC, but many getter methods display values in the user’s local time zone.
Use new Date() when you need a Date object. Calling Date() without new returns a date-time string, which is useful for display but not for calling Date methods such as getTime(), getFullYear(), or toISOString().
Create the current date with JavaScript Date()
To display the current system date and time as a string, call Date() with no arguments. The following example demonstrates the use of Date() as a function.
index.html
<!doctype html>
<html>
<body>
<h1>JavaScript Date Example</h1>
<p id="message"></p>
<script>
<!-- your JavaScript goes here -->
var date = Date();
document.getElementById("message").innerHTML = date;
</script>
</body>
</html>
For most programming tasks, create an actual Date object with new Date(). Then you can read its parts, convert it to ISO format, or compare it with another date.
const now = new Date();
console.log(now.toString());
console.log(now.toISOString());
console.log(now.getFullYear());
toString() displays the date in local time. toISOString() displays the same moment in UTC using an ISO 8601 string.
Get JavaScript time in milliseconds with getTime()
To fetch the current time in milliseconds, use new Date().getTime(). The value is counted from the Unix epoch: 1970-01-01T00:00:00.000Z. This timestamp is useful for comparing dates, measuring elapsed time, and storing a precise moment.
index.html
<!doctype html>
<html>
<body>
<h1>JavaScript Date Example - Get Date in milliseconds</h1>
<p id="message"></p>
<script>
<!-- your JavaScript goes here -->
var date = new Date().getTime();
document.getElementById("message").innerHTML = date;
</script>
</body>
</html>
You can also use Date.now() when you need only the current timestamp and do not need to create a Date object.
const timestamp = Date.now();
console.log(timestamp);
Create a JavaScript Date for a specific day and time
You may initialize the Date object with milliseconds, a date string, or separate date and time components. When using numeric components, the month is zero-based: 0 is January, 1 is February, and 11 is December.
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Following is an example demonstrating the three Constructors mentioned above.
index.html
<!doctype html>
<html>
<body>
<h1>JavaScript Date Constructors Example</h1>
<p id="message"></p>
<script>
<!-- your JavaScript goes here -->
var msg = "";
var date1 = new Date(1516993680832);
var date2 = new Date("Thu Sep 24 2020 14:41:22 GMT+0530 (IST)");
var date3 = new Date(2014, 6, 5, 15, 22, 56, 256);
msg += "date1 : ";
msg += date1;
msg += "<br>";
msg += "date2 : ";
msg += date2;
msg += "<br>";
date.setTime(1516993680832);
msg += "date3 : ";
msg += date3;
msg += "<br>";
document.getElementById("message").innerHTML = msg;
</script>
</body>
</html>
In the numeric constructor above, new Date(2014, 6, 5, ...) represents 5 July 2014, not 5 June 2014, because the month value starts from 0. The line date.setTime(...) is not required unless a variable named date already exists. A cleaner version is shown below.
const fromMillis = new Date(1516993680832);
const fromIsoString = new Date("2020-09-24T09:11:22.000Z");
const fromLocalParts = new Date(2014, 6, 5, 15, 22, 56, 256);
console.log(fromMillis.toISOString());
console.log(fromIsoString.toISOString());
console.log(fromLocalParts.toString());
Prefer ISO 8601 strings such as 2020-09-24T09:11:22.000Z when a date string must represent a precise moment. Short or informal strings can be parsed differently across environments.
Read year, month, day, and time from a JavaScript Date object
JavaScript Date provides getter methods for reading individual parts of a date. The local-time methods are commonly used for UI display. UTC methods are useful when you need a time-zone-independent value.
| Task | Local time method | UTC method |
|---|---|---|
| Year | getFullYear() | getUTCFullYear() |
| Month | getMonth() returns 0-11 | getUTCMonth() returns 0-11 |
| Day of month | getDate() | getUTCDate() |
| Hours | getHours() | getUTCHours() |
| Minutes | getMinutes() | getUTCMinutes() |
| Seconds | getSeconds() | getUTCSeconds() |
const date = new Date("2026-06-28T10:05:30.000Z");
console.log(date.getFullYear());
console.log(date.getMonth() + 1);
console.log(date.getDate());
Add 1 to getMonth() when showing a human-readable month number.
Format a JavaScript Date as YYYY-MM-DD
A common requirement is to convert a Date object to a YYYY-MM-DD string. Use padStart(2, "0") so that single-digit months and days become two digits.
const date = new Date("2026-06-28T10:05:00.000Z");
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
const day = String(date.getUTCDate()).padStart(2, "0");
console.log(`${year}-${month}-${day}`);
2026-06-28
Use the UTC methods when the date string should be based on UTC. Use getFullYear(), getMonth(), and getDate() when the formatted value should match the user’s local calendar date.
Compare two JavaScript dates using timestamps
Because Date objects store time internally as milliseconds, comparing dates is usually easiest after converting them with getTime(). This avoids comparing formatted strings.
const start = new Date("2026-06-01T00:00:00.000Z");
const end = new Date("2026-06-28T00:00:00.000Z");
const differenceInMilliseconds = end.getTime() - start.getTime();
const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);
console.log(differenceInDays);
27
Handle Invalid Date values in JavaScript
If JavaScript cannot parse the supplied value, it creates an Invalid Date object. The object still exists, but its timestamp is NaN. Check the timestamp before using the value in calculations or display.
const date = new Date("not a date");
if (Number.isNaN(date.getTime())) {
console.log("Invalid date value");
}
Invalid date value
JavaScript Date mistakes to avoid
- Do not call
Date().getTime().Date()returns a string. Usenew Date().getTime()orDate.now(). - Do not forget that
getMonth()and the numeric Date constructor use zero-based months. - Do not rely on informal date strings when the value must work consistently. Use ISO 8601 strings for precise date-time values.
- Remember that
toISOString()returns UTC time, not local time. - Be careful with setter methods such as
setDate()andsetTime()because they modify the same Date object.
Official JavaScript Date references
For deeper reference, see the MDN JavaScript Date documentation, the MDN Date constructor reference, and the ECMAScript Date specification.
FAQs on JavaScript Date objects
What is the difference between Date() and new Date() in JavaScript?
Date() returns the current date and time as a string. new Date() creates a Date object, so you can call methods such as getTime(), getFullYear(), and toISOString().
Why does getMonth() return one month less than expected?
getMonth() returns a zero-based month number. January is 0, February is 1, and December is 11. Add 1 when displaying the month to users.
How do I get the current timestamp in JavaScript?
Use Date.now() or new Date().getTime(). Both return the current time in milliseconds from 1970-01-01T00:00:00.000Z.
How can I format a JavaScript Date as YYYY-MM-DD?
Read the year, month, and day with getter methods, add 1 to the month, and use padStart(2, "0") for two-digit month and day values.
Why does toISOString() show a different time?
toISOString() returns the date and time in UTC. If your local time zone is ahead of or behind UTC, the hour or even the calendar date may look different from toString().
Editorial QA checklist for this JavaScript Date tutorial
- Confirm every JavaScript Date example uses
new Date()when Date object methods are called. - Check that explanations mention zero-based months wherever
getMonth()or numeric constructors appear. - Verify output-only examples use the
outputcode block class. - Keep date-string examples in ISO 8601 format when the value represents a precise moment.
- Test copied examples in a browser console or Node.js before publishing updates.
What to remember about JavaScript Date
In this JavaScript Tutorial, we learned how to create a JavaScript Date, get the current timestamp, initialize a Date object with specific values, read date parts, format a date as YYYY-MM-DD, compare dates, and avoid common Date parsing and time-zone mistakes.
TutorialKart.com