0

I have a model in my database that contains an array called "AvailableDays" [0...6]. 0 = Sunday & 6 = Saturday. I am looking to convert this day number of the week to the date of day in the current week.

For example, this is the logic broken down

  1. Retrieve the list of available days (const availableDays = [0,2,4,6])
  2. Get the current DATE (const today = new Date('2021-08-20');)
  3. Covert day numbers to dates (output =['15-08-2021', '17-08-2021', '19-08-2021', '21-08-2021'])
Phil
  • 157,677
  • 23
  • 242
  • 245
Tom Dickson
  • 540
  • 6
  • 19
  • I would actually suggest _changing_ your database design here. Instead of storing the number day of the week, store actual proper dates. – Tim Biegeleisen Aug 20 '21 at 03:30
  • 3
    Does this answer your question? [How to get the day of the week from the day number in JavaScript?](https://stackoverflow.com/questions/9677757/how-to-get-the-day-of-the-week-from-the-day-number-in-javascript) – futur Aug 20 '21 at 03:35
  • @TimBiegeleisen I assume he has some kind of scheduling app where the available days repeat evey week and he wants to find the dates in the current week. – see sharper Aug 20 '21 at 05:13

4 Answers4

5

What you can do is get the day-of-the-week from the given Date instance and work out the offset from your available day.

Then subtract that offset in days from the given date to produce your result.

const transformDate = (date, day) => {
  const offset = date.getDay() - day
  
  const d = new Date(date)
  d.setDate(d.getDate() - offset)
  return d
}

const availableDays = [0,2,4,6]
const today = new Date("2021-08-20")

console.log(availableDays.map(day => transformDate(today, day)))
Phil
  • 157,677
  • 23
  • 242
  • 245
1

You can generate all the days in weeks and then get the dates using availableDays.

const getWeekDays = (current) => {
    current.setDate((current.getDate() - current.getDay() - 1));
    return Array.from({ length: 7 }, (_, i) => {
      current.setDate(current.getDate() + 1)
      return new Date(current).toLocaleDateString('en-CA');
    });
  },
  today = new Date('2021-08-20'),
  weekDays = getWeekDays(today),
  availableDays = [0, 2, 4, 6],
  availableDates = availableDays.map(day => weekDays[day]);
console.log(availableDates);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • 2
    On the input side, '2021-08-20' is parsed as UTC. Then in *getWeekDays*, *toISOString* is used to get the date parts. So users with a negative offset see dates one day later than expected (16, 18, 20, 22 Aug) whereas users with a positive offset see expected dates (15, 17, 19, 21). Also, getting an array of all week days to then filter out the unwanted ones is somewhat inefficient. – RobG Aug 20 '21 at 12:34
1

Was able to solve this myself. I am now able to wrap this into a availableDates.map() and return an array of dates using the below logic.

var availableDay = 0
var d = new Date(),
    day = d.getDay(), // 0 ... 6
    calcAvailableDay = day-availableDay,
    diff = d.getDate() - calcAvailableDay,
    output = new Date(d.setDate(diff));
console.log(output)
Tom Dickson
  • 540
  • 6
  • 19
0

JavaScript getDay method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

So what you have to do is connect this index with your availableDays values.

Logic

  • Get current date, month, year and the index of todays date.
  • Loop through the availableDays array, and create new dates with the difference between the current day calculated with getDay value and the day value specified in your array.
  • Make use of some logic to reperesent those date object in specified format. I took support from this post to format your date string.

const availableDays = [0,2,4,6];
const today = new Date();
const currentDay = today.getDay();
const currentDate = today.getDate();
const currentMonth = today.getMonth();
const currentYear = today.getFullYear();
formatDateToString = (date) => String(date.getDate()).padStart(2, '0') + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + date.getFullYear();
const output = availableDays.map((day) => formatDateToString(new Date(currentYear, currentMonth, currentDate - (currentDay - day))));
console.log(output);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49