Format a long, readable date in JavaScript
Intermediate · 3 min read
"Thursday, 15 January 2026" is how a date looks on an invitation, a booking confirmation or the top of a newsletter. It is much friendlier than 2026-01-15. Building it yourself is a good way to practise reading every part of a date.
In this tutorial you will write longDate, shortDate and longDateTime. Together they produce Thursday, 15 January 2026, Thu, 15 Jan 2026 and Thursday, 15 January 2026 at 2:30 PM. The day comes first, the way most of the world writes it. You need to know how to make a date with new Date(year, month, day, hour, minute) and that months count from 0.
Getters return numbers, not names
A date can tell you its parts, but only as numbers:
const afternoon = new Date(2026, 0, 15, 14, 30);
console.log(afternoon.getDay()); // 4
console.log(afternoon.getDate()); // 15
console.log(afternoon.getMonth()); // 0
console.log(afternoon.getHours()); // 14
getDay() is the day of the week, from 0 for Sunday to 6 for Saturday. getDate() is the day of the month. The names are easy to mix up, so it is worth reading them twice. getMonth() is 0 for January.
To turn numbers into names, keep the names in arrays in the same order. Then the number is the position in the array.
Here is the exercise. Try it on your own first if you like, then read on and we will build it together.
45 A long, readable date by hand
fairProduce Thursday, 15 January 2026 and a version with the time. Day-first order, since that is what most of the world writes.
Stuck? Three hints, smallest first
- 1. You already have the weekday and month arrays; this is assembly.
- 2. Do not pad the day of the month in a long format —
15 January, not15with a leading zero. - 3. The locale-aware version is one call, and its output depends on the machine, so it can be shown but not asserted.
Step 1: the name lists
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
console.log(DAY_NAMES[afternoon.getDay()]); // 'Thursday'
console.log(MONTH_NAMES[afternoon.getMonth()]); // 'January'
Sunday comes first in DAY_NAMES because getDay() gives 0 for Sunday. If you start the list with Monday, every name is off by one.
Step 2: build the long date
Now join the pieces with the right spaces and commas:
const longDate = (date) =>
DAY_NAMES[date.getDay()] + ', ' + date.getDate() + ' ' +
MONTH_NAMES[date.getMonth()] + ' ' + date.getFullYear();
console.log(longDate(afternoon)); // 'Thursday, 15 January 2026'
console.log(longDate(new Date(2026, 1, 5))); // 'Thursday, 5 February 2026'
There is no leading zero on the day. "05 February" looks like a machine wrote it. "5 February" looks like a person did.
Step 3: the short version
slice(0, 3) keeps the first three letters of a string, which gives the usual short names:
const shortDate = (date) =>
DAY_NAMES[date.getDay()].slice(0, 3) + ', ' + date.getDate() + ' ' +
MONTH_NAMES[date.getMonth()].slice(0, 3) + ' ' + date.getFullYear();
console.log(shortDate(afternoon)); // 'Thu, 15 Jan 2026'
Step 4: the 12-hour clock, and the midnight trap
getHours() uses a 24-hour clock, from 0 to 23. To show "2:30 PM", you need the remainder after dividing by 12. A first try:
console.log(14 % 12); // 2
console.log(0 % 12); // 0
14 becomes 2, which is right. But midnight, hour 0, becomes 0, and so does noon, hour 12. Nobody says "0:00 AM". On a 12-hour clock both of those read 12. So when the remainder is 0, use 12 instead:
const pad2 = (n) => String(n).padStart(2, '0');
function longDateTime(date) {
const h24 = date.getHours();
const h12 = h24 % 12 === 0 ? 12 : h24 % 12;
const suffix = h24 < 12 ? 'AM' : 'PM';
return longDate(date) + ' at ' + h12 + ':' + pad2(date.getMinutes()) + ' ' + suffix;
}
console.log(longDateTime(afternoon)); // 'Thursday, 15 January 2026 at 2:30 PM'
console.log(longDateTime(new Date(2026, 0, 15, 0, 0))); // 'Thursday, 15 January 2026 at 12:00 AM'
The hours before 12 are AM, the rest are PM. The minutes do get a leading zero, through pad2, because "2:5 PM" would be confusing.
Step 5: the built-in version
JavaScript can also do this for you:
console.log(typeof afternoon.toLocaleDateString(undefined, { dateStyle: 'full' })); // 'string'
toLocaleDateString with dateStyle: 'full' writes a long date in the language and order the reader's computer is set to. That is great for real apps, because it translates for you. But its exact output depends on that computer's settings, so you can't promise what it prints. Your own longDate always gives the same text, which makes it easy to test.
The full solution
The panel has the two name lists and all three formatters. It prints each format, plus the built-in version for your own machine. The checks cover a Sunday, a day with no leading zero, the short form, an afternoon time and midnight. Press Run to see them.
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
const pad2 = (n) => String(n).padStart(2, '0');
// Day first: "Thursday, 15 January 2026".
const longDate = (date) =>
DAY_NAMES[date.getDay()] + ', ' + date.getDate() + ' ' +
MONTH_NAMES[date.getMonth()] + ' ' + date.getFullYear();
const shortDate = (date) =>
DAY_NAMES[date.getDay()].slice(0, 3) + ', ' + date.getDate() + ' ' +
MONTH_NAMES[date.getMonth()].slice(0, 3) + ' ' + date.getFullYear();
function longDateTime(date) {
const h24 = date.getHours();
const h12 = h24 % 12 === 0 ? 12 : h24 % 12;
const suffix = h24 < 12 ? 'AM' : 'PM';
return longDate(date) + ' at ' + h12 + ':' + pad2(date.getMinutes()) + ' ' + suffix;
}
const afternoon = new Date(2026, 0, 15, 14, 30);
console.log(longDate(afternoon));
console.log(shortDate(afternoon));
console.log(longDateTime(afternoon));
// The built-in, which translates and reorders for the reader's locale. Shown
// only: on a machine set to fr-FR this prints "jeudi 15 janvier 2026".
console.log('toLocaleDateString:', afternoon.toLocaleDateString(undefined, { dateStyle: 'full' }));
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the long form, day first', longDate(afternoon), 'Thursday, 15 January 2026');
test('a Sunday in March', longDate(new Date(2026, 2, 1)), 'Sunday, 1 March 2026');
test('no leading zero on the day', longDate(new Date(2026, 1, 5)), 'Thursday, 5 February 2026');
test('the short form', shortDate(afternoon), 'Thu, 15 Jan 2026');
test('with the time, in twelve-hour form', longDateTime(afternoon), 'Thursday, 15 January 2026 at 2:30 PM');
test('midnight reads as 12 AM', longDateTime(new Date(2026, 0, 15, 0, 0)), 'Thursday, 15 January 2026 at 12:00 AM');
test('the locale version returns a string, which is all that can be promised', typeof afternoon.toLocaleDateString(undefined, { dateStyle: 'full' }), 'string');
test('and it is not empty', afternoon.toLocaleDateString().length > 0, true);
Assembly, with one judgement call: no leading zero. 05 February looks like machine output, 5 February looks like writing.
The last two assertions are as far as a test can honestly go with toLocaleDateString. Its output depends on the machine's locale and on which languages the runtime's ICU data includes, so asserting 'Thursday, 15 January 2026' against it would pass on the machine that wrote it and fail somewhere else. In production it is the right tool, because it translates and reorders for each reader; in a test suite, assert your own formatter and let the built-in be shown.
Common mistakes
- Using
getDate()for the weekday.getDate()is the day of the month. The weekday isgetDay(). - Starting
DAY_NAMESon Monday.getDay()gives 0 for Sunday, so every name comes out one day off. - Showing midnight as "0:00 AM".
0 % 12is 0. Use 12 when the remainder is 0.
Try it yourself
- Write
usLongDate(date)that puts the month first: "Thursday, January 15, 2026". - Add seconds to
longDateTime, padded to two digits. - Print
afternoon.toLocaleDateString('fr-FR', { dateStyle: 'full' })and compare it with your own format.