Javascript Date get week day as String

Introduction

We would like to show a weekday in string for a date object.

Write a function getWeekDay(date) to show the weekday in short format:

  • MO
  • TU
  • WE
  • TH
  • FR
  • SA
  • SU
2020 March 25 should return WE as it is a Wednesday.

Hint

The method date.getDay() returns the number of the weekday, starting from sunday.

Make an array of weekdays, and we can get the proper day name by its number.



function getWeekDay(date) {
  let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];

  return days[date.getDay()];
}

let date = new Date(2020, 2, 25); // 25 March 2020
console.log( getWeekDay(date) ); //  WE



PreviousNext

Related