Tom Dickson
Tom Dickson

Reputation: 622

Get the date from day number of week Javascript

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'])

Upvotes: 0

Views: 1495

Answers (4)

Hassan Imam
Hassan Imam

Reputation: 22524

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);

Upvotes: 1

Phil
Phil

Reputation: 164736

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)))

Upvotes: 5

Nitheesh
Nitheesh

Reputation: 19986

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);

Upvotes: 0

Tom Dickson
Tom Dickson

Reputation: 622

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)

Upvotes: 1

Related Questions