Files
30-seconds-of-code/snippets/dayName.md
Angelos Chalaris 0e3d752c22 Update dayName.md
2020-10-04 00:59:36 +03:00

19 lines
509 B
Markdown

---
title: dayName
tags: date,beginner
---
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 parameter 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'
```