Home > Software design >  How to find the days of the week in JavaScript
How to find the days of the week in JavaScript

Time:09-27

I am trying to find the weekdays from today to Sunday. Since today is Monday I want to display the dates from Monday till Sunday, but tomorrow, I want my programme works from Tuesday to Sunday.

dateSets() {
            let firstDayOfWeek = "";
            let dates = [];
            firstDayOfWeek = new Date();
                let sunday = new Date();
                sunday.setDate(sunday.getDate() - sunday.getDay()   7);
                const diff = sunday.getDate() - firstDayOfWeek.getDate();
                dates.push(new Date());
                for (let i = 0; i < diff; i  ) {
                    dates.push(
                        new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate()   1))
                    );
                }
            return dates;
        },

And here is the other function to find the date of the week:

getDateOfWeek(week, year) {
            let simple = new Date(year, 0, 1   (week - 1) * 7);
            let dow = simple.getDay();
            let weekStart = simple;
            if (dow <= 4) weekStart.setDate(simple.getDate() - simple.getDay()   1);
            else weekStart.setDate(simple.getDate()   8 - simple.getDay());
            return weekStart;
        },

But it doesn't work that I expected, in dataset, only Monday is being displayed but not other dates and I don't understand the reason. If you can help me with this, I would be really glad. Thanks...

CodePudding user response:

This is kind of your choice but if you install NodeJS and open your folder in cmd and type in 'npm init' and 'npm I days' and open your editor and type in

var days = require('days');
console.log(days); // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

console will reveal all of the days of the week and if you want a specific day you can do the following

var days = require('days');
console.log(days[0]);  //Sunday

if you need help with installing NodeJS watch a YouTube video or reply to this comment I will help

CodePudding user response:

let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
    let sunday = new Date();
    sunday.setDate(sunday.getDate() - sunday.getDay()   7);
    //const diff = sunday.getDate() - firstDayOfWeek.getDate();
    //Try this code
    var timeDiff = Math.abs(sunday.getTime() - firstDayOfWeek.getTime());
    var diff = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    dates.push(new Date());
    for (let i = 0; i < diff; i  ) {
        dates.push(
            new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate()   1))
        );
    }
return dates;

CodePudding user response:

function getWeekDates(){
  const dates = [new Date()]; // today
  const curr = new Date();
  const remDaysCount = 7-curr.getDay();
  for(let i=1; i<= remDaysCount; i  ){
    // increase current Date by 1 and set to current Date
      const nextDate = curr.setDate(curr.getDate() 1);
    dates.push(new Date(nextDate));
  }
  return dates;
}

console.log(getWeekDates());

CodePudding user response:

Your issue is here:

const diff = sunday.getDate() - firstDayOfWeek.getDate()

Currently the date is 27 Sep and next Sunday is 3 Oct so diff is -4 and the for loop test i < diff is false from the start. Consider using a loop and increment the date from today until it gets to Sunday.

function getDaysToSunday(date = new Date()) {
  let d = new Date( date);
  let result = [];
  do {
    result.push(new Date( d));
    d.setDate(d.getDate()   1);
  } while (d.getDay() != 1)
  return result;
}

console.log(getDaysToSunday().map(d=>d.toDateString()));

  • Related