0

I have a diferent number of days example (1=monday,2=tuesday.......) and i need to return the date o this day in the current week

For example: if i send number 3 to my function, in this current week, today is sunday 23 , and i need that my function give me the date of the day 3 in this week, in this case the wednesday day was june-19 . other example i pass the number 5 to my function the function return me the date of the day 5 this week (friday)= june-21 (2019/06/21)

something like that

let currentDate = new Date().toISOString().slice(0, 10)

3
  • What have you tried? toISOString gives you a UTC string, you probably don't want that. getDate gets the current day in the month, getDay gets you the current day in the week, and setDay sets it to a particular day in the week. That's all you need to do the job. ;-) Commented Jun 23, 2019 at 20:16
  • Yes but you understand what i need ? Commented Jun 23, 2019 at 20:21
  • Yes, but SO is to help fix code you've written, not write it for you. You need something like d.setDate(d.getDate() - d.getDay() + n) where d is the current date and n is the day number you seek. Commented Jun 23, 2019 at 20:30

1 Answer 1

1

The strategy is fairly straight forward:

  1. new Date() gets you the current date
  2. The getDay method returns the day number of the date's day of the week
  3. The getDate method returns the day number of the date's day of the month
  4. The setDate method sets the day number of the date's day of the month

So you get the current date, subtract the current day number, then add the day number you want to the date. This will also wrap to previous and following months, e.g.

/* Given a week day number, return the date for that
 * day in the current week.
 *
 * @param {number} dayNumber - number of day in week.
 *   If 0, returns Sunday at start of week
 *   If 7, returns Sunday at end of week
 *   Otherwise 1 Mon, 2 Tue, etc.
 *   If not an integer in range 0 to 7 returns undefined
 * @returns {number|undefined} date of supplied day number
 */
function getDateForDayInWeek(dayNumber) {
  dayNumber = Number(dayNumber);
  // Validate input
  if (dayNumber < 0 ||
      dayNumber > 7 ||
      parseInt(dayNumber) != dayNumber) {
    return; // undefined
  }
  let d = new Date();
  d.setDate(d.getDate() - d.getDay() + dayNumber);
  return d.getDate();
}

// Examples
console.log('Today is ' + 
  new Date().toLocaleString(undefined, {
    month:'long', weekday:'long', day:'numeric'
  })
);
// Sample values
[0,  // Sunday at start of week 
 3,  // Wednesday
 7,  // Sunday end of week
 23, -2, 2.4 // All invalid, return undefined
].forEach(
  n => console.log(n + ':' + getDateForDayInWeek(n))
);

You might want the function to return a Date, then you can do more with it, including just getting the date, but also the month and day name, etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.