Get the day of the week in JavaScript without a library
Tags: javascript, webdev, beginners
Published: 2023-02-05 14:14:10

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/javascript-day-of-week) 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: 

```js
// 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:

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

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

```js
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:

```js
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:

```js
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!

```js
// 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.

```js
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:

```js
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:**

- [Convert a dd/mm/yyyy string to a Date object in JavaScript](https://www.decodingweb.dev/convert-dd-mm-yyyy-string-to-javascript-date-object)
- [Subtract days from a date in JavaScript without a library](https://www.decodingweb.dev/javascript-subtract-days-from-date)
- [How to add days to a date in JavaScript (without a library)](https://www.decodingweb.dev/javascript-date-add-days)
- [How to get yesterday's date in JavaScript without a library](https://www.decodingweb.dev/javascript-yesterday-date)
- [Add commas to numbers in JavaScript (Explained with examples)](https://www.decodingweb.dev/add-commas-to-numbers-in-javascript)