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.Β
π§ Debugging Jam
Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat β subscribe for the ultimate coding groove!" Let the bug-hunting begin! π΅π»π
The Date object can give us the day of the week in the following formats:
Psssst! Do you want to learn web development in 2023?
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.
Never miss a guide like this!
Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesnβt change the cost youβll pay.