30秒学会 JavaScript 片段 · 2023年11月25日

30秒学会 JavaScript 片段 – Day name

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.

代码实现

const dayName = (date, locale) =>
  date.toLocaleDateString(locale, { weekday: 'long' });

dayName(new Date()); // 'Saturday'
dayName(new Date('09/23/2020'), 'de-DE'); // 'Samstag'

翻译自:https://www.30secondsofcode.org/js/s/day-name