Files
30-seconds-of-code/snippets/js/s/day-name.md
2023-05-07 16:07:29 +03:00

24 lines
595 B
Markdown

---
title: Day name
type: snippet
language: javascript
tags: [date]
cover: interior
dateModified: 2020-11-01T20:50:57+02:00
---
Gets the name of the weekday from a `Date` object.
- Use `Date.prototype.toLocaleDateString()` with the `{ weekday: 'long' }` option to retrieve the weekday.
- Use the optional second argument to get a language-specific name or omit it to use the default locale.
```js
const dayName = (date, locale) =>
date.toLocaleDateString(locale, { weekday: 'long' });
```
```js
dayName(new Date()); // 'Saturday'
dayName(new Date('09/23/2020'), 'de-DE'); // 'Samstag'
```