Get the weekday name (long and short) in JavaScript
Beginner · 4 min read
Calendars, receipts and chat apps all show the day of the week: "Thursday", or "Thu" where space is tight. JavaScript can tell you which day a date falls on, but only as a number. In this tutorial you will turn that number into a name, in a long form and a short form.
The trick takes one array and one line of code. The only thing to get right is where the week starts, and it isn't where most people expect. You need to know how to make a date with new Date(year, month, day) and how arrays and indexes work.
getDay() gives a number
Every date has a getDay() method. It returns the day of the week as a number from 0 to 6:
- 0 is Sunday
- 1 is Monday
- 2 is Tuesday
- 3 is Wednesday
- 4 is Thursday
- 5 is Friday
- 6 is Saturday
Notice that 0 is Sunday, not Monday. That is fixed in JavaScript and you can't change it.
console.log(new Date(2026, 0, 15).getDay()); // 4
15 January 2026 (January is month 0) is a Thursday, so you get 4.
Arrays turn numbers into names
An array keeps its items in order, and each item has an index, starting at 0. So if you put the day names in an array in the same order as the numbers above, the number from getDay() points straight at the right name.
Here is the exercise. Try it first if you like, then read on and we will build it together.
3 The name of the day
easyTurn a date into its weekday name, such as Thursday, and into a three-letter short form.
Stuck? Three hints, smallest first
- 1.
getDay()gives 0 to 6, so an array of names indexed by it is all you need. - 2. The array has to start at Sunday, because that is what 0 means.
- 3. A short form is the first three letters of the long one — do not keep a second array in step by hand.
Step 1: the array of names
Write the seven names, starting with Sunday because that is what 0 means:
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
console.log(DAY_NAMES[4]); // 'Thursday'
console.log(DAY_NAMES.length); // 7
The name is in capital letters because it is a fixed list that never changes. That is a common naming habit for values like this.
Step 2: look up a date's name
Use getDay() as the index:
const dayName = (date) => DAY_NAMES[date.getDay()];
const thursday = new Date(2026, 0, 15);
const sunday = new Date(2026, 0, 18);
console.log(dayName(thursday)); // 'Thursday'
console.log(dayName(sunday)); // 'Sunday'
dayName is an arrow function: it takes date and returns the name in one line.
Step 3: the wrong turn, starting at Monday
Many people think of Monday as the first day of the week, so it is tempting to write the array that way:
const MONDAY_FIRST = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
console.log(MONDAY_FIRST[thursday.getDay()]); // 'Friday'
Every answer is now one day out, and Sunday's 0 picks "Monday". The array's order must match what getDay() returns, so it has to start at Sunday. If your app needs weeks that start on Monday, keep this array as it is and convert the number elsewhere.
Step 4: the short name
For "Thu", you could keep a second array of short names. But then there are two lists to keep in step, and a typo in one won't show up in the other. Instead, take the first three letters of the long name:
const shortDayName = (date) => dayName(date).slice(0, 3);
console.log(shortDayName(thursday)); // 'Thu'
console.log(shortDayName(sunday)); // 'Sun'
slice(0, 3) copies the characters at positions 0, 1 and 2. Because the short name is built from the long one, the two can never disagree.
Step 5: the week wraps around
Seven days later, the name repeats:
console.log(dayName(new Date(2026, 0, 22))); // 'Thursday'
The built-in, translated version
Browsers and node can also give you the name in the reader's own language, with toLocaleDateString:
console.log(thursday.toLocaleDateString(undefined, { weekday: 'long' }));
undefined means "use this computer's language". On an English system that prints Thursday; on a French one it prints jeudi. That is the right choice for a real interface, but it isn't something a test can pin down, because the answer depends on the machine.
The full solution
The panel has the array, both functions and the locale version (printed only). Press Run and compare the output.
// getDay() returns 0 for Sunday, so index 0 must be Sunday.
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const dayName = (date) => DAY_NAMES[date.getDay()];
const shortDayName = (date) => dayName(date).slice(0, 3); // derived, so it cannot drift
const thursday = new Date(2026, 0, 15); // 15 January 2026
const sunday = new Date(2026, 0, 18); // 18 January 2026
console.log('15/01/2026 ->', dayName(thursday), '/', shortDayName(thursday));
console.log('18/01/2026 ->', dayName(sunday), '/', shortDayName(sunday));
// The locale-aware version. Shown only: its output depends on the machine's
// language settings, so it is not something a test can pin down.
console.log('toLocaleDateString ->', thursday.toLocaleDateString(undefined, { weekday: 'long' }));
Output
7 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 7 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('15 January 2026 is a Thursday', dayName(thursday), 'Thursday');
test('and its short form', shortDayName(thursday), 'Thu');
test('18 January 2026 is a Sunday', dayName(sunday), 'Sunday');
test('Sunday is index 0, which is why the array starts there', sunday.getDay(), 0);
test('Saturday is index 6', dayName(new Date(2026, 0, 17)), 'Saturday');
test('there are seven names', DAY_NAMES.length, 7);
test('the week wraps: seven days on is the same name', dayName(new Date(2026, 0, 22)), 'Thursday');
An array indexed by getDay() is the whole trick, and the only thing to get right is that index 0 is Sunday. If your product treats Monday as the first day of the week, do not reorder this array — you would break the indexing. Convert instead, which exercise 34 does.
toLocaleDateString(undefined, { weekday: 'long' }) gives a translated name and is the right answer in a real interface. It is shown here rather than asserted because its output depends on the reader's locale and on which languages their runtime shipped with.
Common mistakes
- Starting the array at Monday.
getDay()returns 0 for Sunday, so a Monday-first array gives every day the wrong name. - Using
getDate()instead ofgetDay().getDate()is the day of the month, 1 to 31. From the 7th of the month on, it reads past the end of the 7-item array and you getundefined. - Keeping a separate array of short names. It can drift out of step with the long one. Derive it with
slice(0, 3).
Try it yourself
- Write
isFriday(date)usingdayName. - Write a two-letter version, so Thursday becomes
'Th'. - Print the names of all seven days from 18 to 24 January 2026 with a
forloop. Which day does it start on?