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 J…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian

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()
  3. Using Date.prototype.getDay()
  4. 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:


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian


Print Share Comment Cite Upload Translate Updates
APA

Reza Lavarian | Sciencx (2023-02-05T14:14:10+00:00) Get the day of the week in JavaScript without a library. Retrieved from https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/

MLA
" » Get the day of the week in JavaScript without a library." Reza Lavarian | Sciencx - Sunday February 5, 2023, https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/
HARVARD
Reza Lavarian | Sciencx Sunday February 5, 2023 » Get the day of the week in JavaScript without a library., viewed ,<https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/>
VANCOUVER
Reza Lavarian | Sciencx - » Get the day of the week in JavaScript without a library. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/
CHICAGO
" » Get the day of the week in JavaScript without a library." Reza Lavarian | Sciencx - Accessed . https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/
IEEE
" » Get the day of the week in JavaScript without a library." Reza Lavarian | Sciencx [Online]. Available: https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/. [Accessed: ]
rf:citation
» Get the day of the week in JavaScript without a library | Reza Lavarian | Sciencx | https://www.scien.cx/2023/02/05/get-the-day-of-the-week-in-javascript-without-a-library/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.