DEV Community

Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

Get the day of the week in JavaScript without a library

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

How would you get the day of the week in JavaScript?

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.

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 🎄
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

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' }))
Enter fullscreen mode Exit fullscreen mode

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

Thanks for reading.


❤️ You might like:

Top comments (1)

Collapse
 
naucode profile image
Al - Naucode

Great article, you got my follow, keep writing!