Javascript Get Yesterdays Day of Week

Try

let yesterday = {
  'Monday': 'Sunday',
  'Tuesday': 'Monday',
  'Wednesday': 'Tuesday',
  'Thursday': 'Wednesday',
  'Friday': 'Thursday',
  'Saturday': 'Friday',
  'Sunday': 'Saturday',
}

console.log(yesterday['Monday']);


One straightforward approach, assuming correct input, would be using arrays:

days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
previousDay = days[(days.indexOf(currentDay)-1+7)%7];

You can use indexOf and return Sun when you exceed array range:

let currentDay = "Mon";

let days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
let prevDay = days[days.indexOf(currentDay) - 1 ] || "Sun";

console.log(prevDay);

Tags:

Javascript