Get the day of the week in JavaScript without a library

If you need to get the day of the week in JavaScript, you can do so with the standard Date object methods – without using a third-party library.

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

The Date object can give us the day of the week in the following formats:

  1. Numerical with getDay()

  2. Full name with toLocaleDateString()

Using Date.prototype.getDay()

The getDay() method returns the day of the week for the specified date (according to the local time).

The return value of getDay() is an integer number between 0 and 6. The number 0 is for Sunday, 1 for Monday, and so on.

Please note, getDay() is different than getDate(), which is used to get the day of the month.

Let’s see some examples:

// Get today
const today = new Date().getDay() // 6 (Saturday)

// Get the Christmas day 🎅
const christmasDay = new Date('2022-12-25').getDay() // 0 (Sunday)

You might need the full name of a day (e.g., Sunday) rather than a number. In that case, you should use the toLocaleDateString() method instead.

Using the Date.prototype.toLocaleDateString()

The toLocaleDateString() method uses the Internationalization API under the hood to provide language-sensitive date formatting.

All you need to do is to provide the locale (e.g., en-EN for English) and specify the weekday format in its options parameters:

const today = new Date().toLocaleDateString('en-EN', { weekday: 'long' })
// Example output: Saturday

The weekday option takes other values as well, including short and narrow:

const today = new Date()
console.log(
   today.toLocaleDateString('en-EN', { weekday: 'short' })
)
// Example output:  Sun

// Or use narrow for displaying days on calendars 🗓️
console.log(
   today.toLocaleDateString('en-EN', { weekday: 'narrow' })
)
// Example output:  S

Changing the locale will give you the day's name in the respective language. For instance, to get today in German, you can do like so:

const today = new Date().toLocaleDateString('de-DE', { weekday: 'long' })

console.log(today)
// Example output:  Samstag

You can also get the day of a specified date. For instance, to get the day of Christmas 2021:

const lastChristmasDay = new Date('2021-12-25').toLocaleDateString('en-EN', { weekday: 'long' })

console.log(lastChristmasDay)
// Expected output:  Saturday 🎄

You can even figure out what day you were born. If you were born on the 1st of October, 1990, you were born on a Monday!

// Birthday 🎂
const birthday = new Date('1990-10-1')

console.log(
  birthday.toLocaleDateString('en-EN', { weekday: 'long' }
)
// output:  Monday

Make a reusable function to get the day of the week in JavaScript

To make the above functionality reusable, you can extend the Date prototype. It's similar to the above approach, but it'll save you a few keystrokes!

Let's see how we can do it.

Date.prototype.getDayAsString = function (locale = 'en-EN', userOptions) {
    // Override default options with user options
    const options = {
        weekday: 'long',
        ...userOptions
    }

    return this.toLocaleDateString(locale, options)
}

Now you can call it on any date object like so:

const today = new Date()

console.log(today.getDayAsString())
console.log(today.getDayAsString('de-DE', { weekday: 'long' }))

Alright, I think that does it! I hope you found this guide helpful.

Thanks for reading.


❤️ You might like: