Dates appear everywhere in an interface: “Posted on 5 November 2026”, “Due in 3 days”, “Last edited at 14:30”. JavaScript stores dates as Date objects that count milliseconds since 1 January 1970, but the raw default display of those objects is long, inconsistent, and rarely what you want to show a user. The good news is that you do not need a library to format date javascript output well. The built-in Intl.DateTimeFormat API handles locale-aware formatting, and a handful of getter methods plus a small padding helper cover fully custom patterns.
This guide walks through creating dates correctly, formatting them with Intl for different locales and styles, building your own format such as YYYY-MM-DD by hand, handling the month-index trap that catches every beginner, and calculating simple relative values like days between dates. If your dates come from an API, you will usually receive them as ISO strings, and the loading side of that work is covered in our Fetch API examples. Formatting for display is the last step before rendering, and doing it consistently across a project prevents the mix of styles that makes an app look unfinished.
Creating Dates Correctly First
Formatting only works well if the Date object itself is correct, and creation has pitfalls. You can build a date from the current moment with new Date(), from an ISO string returned by a server, from separate year, month, and day numbers, or from a timestamp. The number-based constructor is where the famous trap lives: months are zero-indexed, so month 0 is January and month 10 is November, while days and years behave as expected.
// Current date and time
const now = new Date();
console.log(now);
// From an ISO string (recommended for server data)
const fromServer = new Date("2026-11-05T10:30:00");
console.log(fromServer.getFullYear()); // 2026
// From parts: year, monthIndex, day
const birthday = new Date(2026, 10, 5); // 5 November 2026
console.log(birthday.getMonth()); // 10, meaning November
// From a timestamp in milliseconds
const fromTimestamp = new Date(1794133800000);
console.log(fromTimestamp.toISOString());
The ISO string form is the safest for data exchange because it is unambiguous; a string like “05/11/2026” can mean 5 November or May 11 depending on locale, and parsing of non-ISO strings varies between browsers. When using the parts constructor, always add a comment or use clear variable names so the zero-indexed month does not confuse the next reader. The getters shown, getFullYear and getMonth, return local-time values. Their UTC cousins, getUTCFullYear and getUTCMonth, return values in Coordinated Universal Time, which matters when users sit in different time zones and you need a single consistent reference.
Formatting With Intl.DateTimeFormat
Intl.DateTimeFormat is the built-in internationalisation formatter, and it should be your first choice for anything a user reads. You give it a locale such as “en-GB” or “en-US” and an options object describing which parts to show and in what style, and it produces a correctly ordered, correctly punctuated string for that audience, including month names in the right language. Creating the formatter once and reusing it is faster than building it on every call.
const date = new Date(2026, 10, 5, 14, 30);
const gbFormatter = new Intl.DateTimeFormat("en-GB", {
day: "numeric",
month: "long",
year: "numeric"
});
console.log(gbFormatter.format(date)); // "5 November 2026"
const usFormatter = new Intl.DateTimeFormat("en-US", {
day: "numeric",
month: "long",
year: "numeric"
});
console.log(usFormatter.format(date)); // "November 5, 2026"
const shortFormatter = new Intl.DateTimeFormat("en-GB", {
dateStyle: "medium",
timeStyle: "short"
});
console.log(shortFormatter.format(date)); // "5 Nov 2026, 14:30"
Notice how the same date renders differently for the two locales with identical options: British English puts the day first, while American English puts the month first and adds a comma. That reordering, plus translated month names for non-English locales, is work you get for free and would almost certainly get wrong by hand. The options accept values like “numeric”, “2-digit”, “long”, “short”, and “narrow” for each component: weekday, day, month, year, hour, minute, and second. The dateStyle and timeStyle shortcuts bundle sensible option sets and cannot be mixed with individual component options in the same object, so choose one approach per formatter. For a blog card, “medium” date style is a solid default; for a table where space is tight, numeric day and month with a two-digit year may fit better.
Useful Intl Recipes
const date = new Date(2026, 10, 5, 9, 5, 0);
// Weekday only
console.log(
new Intl.DateTimeFormat("en-GB", { weekday: "long" }).format(date)
); // "Thursday"
// Time only, 24-hour clock
console.log(
new Intl.DateTimeFormat("en-GB", {
hour: "2-digit",
minute: "2-digit",
hourCycle: "h23"
}).format(date)
); // "09:05"
// Month and year for a heading
console.log(
new Intl.DateTimeFormat("en-GB", {
month: "long",
year: "numeric"
}).format(date)
); // "November 2026"
These recipes show the range of the API. Asking only for a weekday is perfect for forecast strips. The time-only recipe pins the hour cycle to h23 so output uses the 24-hour clock consistently instead of depending on locale defaults, and the two-digit options guarantee leading zeros. The month-and-year recipe suits archive headings. Storing a few named formatters at the top of a utilities file and importing them wherever dates are displayed keeps an entire project visually consistent, the same way shared helpers keep array handling consistent in our JavaScript array methods cheat sheet.
Building a Custom Format by Hand: YYYY-MM-DD and DD/MM/YYYY
Sometimes you need an exact machine-friendly pattern, such as the ISO-style YYYY-MM-DD used in URLs, file names, and HTML date inputs. Intl does not guarantee that exact shape across locales, so building it manually is the reliable route. The only real work is padding single-digit months and days with a leading zero, which the padStart string method handles neatly.
function formatISODate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
function formatSlashDate(date) {
const day = String(date.getDate()).padStart(2, "0");
const month = String(date.getMonth() + 1).padStart(2, "0");
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
const sample = new Date(2026, 0, 9); // 9 January 2026
console.log(formatISODate(sample)); // "2026-01-09"
console.log(formatSlashDate(sample)); // "09/01/2026"
Three details do all the work here. First, getMonth() returns 0 to 11, so we add 1 to get the human month number 1 to 12. Second, getDate() returns the day of the month, while getDay() returns the day of the week; mixing those two up is a classic bug that produces very strange output. Third, String(...).padStart(2, "0") converts the number to a string and pads it to two characters, so January becomes “01” instead of “1”. The two functions then join the parts with different separators and orders using template literals. A value produced by formatISODate can be assigned directly to an <input type="date"> element, which expects exactly that pattern, and it sorts correctly as plain text, unlike slash formats.
Adding Time to a Custom Format
The same padding approach extends to hours, minutes, and seconds for timestamps in logs, comment threads, and admin tables. Hours from getHours() run from 0 to 23 in local time, so no index adjustment is needed, but padding still matters for a tidy column of times.
function formatDateTime(date) {
const pad = (n) => String(n).padStart(2, "0");
const day = pad(date.getDate());
const month = pad(date.getMonth() + 1);
const year = date.getFullYear();
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
return `${day}/${month}/${year} ${hours}:${minutes}:${seconds}`;
}
console.log(formatDateTime(new Date(2026, 10, 5, 9, 5, 3)));
// "05/11/2026 09:05:03"
Defining a tiny local pad arrow function removes repetition, so each component is one short line. The function assembles a full date-time string with a space between the date and time parts. If you need a 12-hour clock with AM and PM instead, compute the hour with date.getHours() % 12 || 12 and append “AM” when the hour is below 12 and “PM” otherwise. Handle that conversion deliberately, because silently showing 14:30 to an audience that expects 2:30 PM, or the reverse, reads as a bug even when the underlying data is correct. For user-facing times, Intl with an explicit hour cycle is still the safer choice; hand-built strings shine for logs and exports where the format must never vary.
Built-In Shortcuts: toLocaleDateString, toDateString, toISOString
Date objects carry convenience methods that wrap the tools above. toLocaleDateString and toLocaleString accept the same locale and options as Intl and are fine for one-off formatting. toDateString gives a fixed English-style string with no options, useful only for quick debugging. toISOString returns the full UTC timestamp in ISO 8601 format, which is the standard choice for sending dates to a server or storing an exact moment.
const date = new Date(2026, 10, 5, 14, 30, 0);
console.log(date.toLocaleDateString("en-GB")); // "05/11/2026"
console.log(date.toLocaleString("en-GB")); // "05/11/2026, 14:30:00"
console.log(date.toDateString()); // "Thu Nov 05 2026"
console.log(date.toISOString()); // UTC ISO string ending in Z
The locale methods respect the user’s conventions with almost no code, which is why they suit prototypes and simple pages. In larger codebases, explicit Intl formatters are easier to share and test. The toISOString output ends with “Z”, signalling UTC; remember that it can show a different calendar day from local time when the local moment is near midnight, because it converts to UTC first. Saving that ISO string is a good approach when you need to persist a moment, for example with the expiry pattern that stores timestamps alongside values in our localStorage guide.
Working With Relative Time and Date Differences
Interfaces often need differences: how many days until a deadline, or how long ago a comment was posted. Since dates are millisecond counts underneath, subtraction gives you the gap directly, and dividing converts it into days, hours, or minutes. For friendly phrases like “3 hours ago”, Intl.RelativeTimeFormat produces localised text from a number and a unit, sparing you from hand-writing plural rules.
const start = new Date(2026, 10, 1);
const end = new Date(2026, 10, 5);
const diffMs = end - start;
const diffDays = diffMs / (1000 * 60 * 60 * 24);
console.log(diffDays); // 4
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf.format(-3, "day")); // "3 days ago"
console.log(rtf.format(1, "day")); // "tomorrow"
console.log(rtf.format(-2, "hour")); // "2 hours ago"
Subtracting two Date objects coerces them to their millisecond timestamps, so the difference is a plain number of milliseconds. Dividing by 1000 gives seconds, by 60 more gives minutes, and so on up to days. Be careful with daylight saving changes, which can make a calendar day 23 or 25 hours long in local time; for pure calendar-day differences, normalise both dates to midnight UTC before subtracting. The RelativeTimeFormat examples show negative values for the past and positive for the future, and the “auto” numeric style produces words like “tomorrow” and “yesterday” where they exist. For a live “time ago” label on a dashboard that refreshes itself, rebuilding the string on a throttled timer using the approach in our debounce and throttle guide keeps updates cheap.
Common Date Mistakes to Avoid
Four mistakes account for most date bugs. The zero-indexed month has already been mentioned, and it deserves its reputation: new Date(2026, 11, 25) is 25 December, not 25 November. The second is parsing non-ISO strings and getting different results across browsers and locales; always exchange ISO strings or timestamps. The third is mutating a shared Date object with setters like setDate and then wondering why another part of the page changed too; copy first with new Date(original) when you need a modified version. The fourth is comparing dates with equality operators on the objects themselves, which compares references, not moments; compare getTime() values, or subtract one date from the other, instead. An invalid date is also worth checking for: new Date("not a date").getTime() returns NaN, so guard user-entered or server-supplied strings before formatting, or the user will see the literal text “Invalid Date” in the interface.
Conclusion
You can format dates confidently without any library by splitting the job in two. For anything a person reads, use Intl.DateTimeFormat with an explicit locale and options, and reuse named formatters so the whole project speaks consistently. For exact patterns like YYYY-MM-DD, build the string from the getter methods, remember the month-plus-one correction, and pad with padStart. Keep exchanges in ISO format, compare dates by their time values, and check for invalid dates before display. With those habits, dates stop being a recurring source of bugs and become just another reliable part of your interface.



