Home > database >  Javascript function which returns the date of the next specific weekday
Javascript function which returns the date of the next specific weekday

Time:01-23

I am writing a function in JavaScript which takes in a weekday string and a date object and returns the date of the next weekday.

I am pretty inexperienced with working with dates, and what I have written seems a bit complex - and am thinking there is probably a far simpler way to write the code.

Would anyone mind having a look and seeing if they could provide some feedback on how it could be improved?

getLessonDate() would be called with the weekday string and date object, and use the helper functions to derive the days til the next weekday, and ultimately provide the next weekday date object.

Thanks so much in advance

// returns remaining days left in the week (ending
// Saturday). Does not include the day that is
// passed in (dayInt)
const remaingDaysInTheWeek = (dayInt) => 6 - dayInt;

// returns the numbers of days until tarket weekday (including
// weekday passed to fn)
const dayOfTheWeekCount = (weekday) => dow.indexOf(weekday)   1;

// fn to calculate how many days until next weekday
const calculateDaysUntilNextWeekWeekday = (todayInt, lessonWeekdayString) =>
  remaingDaysInTheWeek(todayInt)   dayOfTheWeekCount(lessonWeekdayString);

const getLessonDate = (lessonWeekdayString, todayDateObj) => {
  const todayWeekdayInt = todayDateObj.getDay();
  const lessonDayInt = dow.indexOf(lessonWeekdayString);
  let lessonDate = todayDateObj;

  const daysToAdd =
    todayWeekdayInt <= lessonDayInt
      ? lessonDayInt - todayWeekdayInt
      : calculateDaysUntilNextWeekWeekday(todayWeekdayInt, lessonWeekdayString);

  lessonDate.setDate(todayDateObj.getDate()   daysToAdd);

  return lessonDate;
};

CodePudding user response:

you could use moment.js

const func = (lessonWeekdayString, todayDateObj)=>{
  // could be date obj or string ('YYYY-MM-DD HH:mm:ss'|'YYYY-MM-DD') 
  // or undefined to use the actual date
  const days={
    'monday':1,
    'tuesday':2,
    'wednesday':3,
    'thursday':4,
    'friday':5,
    'saturday':6,
    'sunday':7
  };
  return moment(todayDateObj).day(days[lessonWeekdayString]);
}

https://codesandbox.io/s/quizzical-ace-csl2m3

CodePudding user response:

Logic goes like this:

  1. Start with tomorrow (you're looking for the next weekday)
  2. Loop until your getDay matches the correct day
  3. Return that date
const dow = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

const nextDay = (dayString, date) => {
   const dayIndex = dow.indexOf(dayString)
   const nextDate = new Date(date)
   nextDate.setDate(nextDate.getDate() 1) // start with tomorrow
   while(nextDate.getDay() !== dayIndex) {
     nextDate.setDate(nextDate.getDate() 1)
   }
   return nextDate
}

You could do it with math operations, too, as you did. But I personally find the loop easier to grok - there’s very little logic in the loop function to read

Weekdays don’t really have anything to do with this either, you want a function that returns the next date given a certain day string, it’s not particularly relevant that the day string will always be a weekday when the function is used. I have a suspicion that a requirement of the next “weekday” made the code more complicated than necessary.

  • Related