1

I use this function to get the current week

 Date.prototype.getWeek = function(start)
{
  start = start || 1;
  today = new Date(this.setHours(0, 0, 0, 0));

  var day = today.getDay() - start;
  date = today.getDate() - day;

  var giorni = [];
  var currdate;
  for(var i = 0;i<7;i++){  
    if(getNumberOfDays(today.getFullYear(),today.getMonth()) == currdate){
      var changed=1;
      var newdate=new Date(today.getFullYear(),today.getMonth()+1,1);
      giorni.push(newdate);      
    }else{
      if(changed==1){
        var dt = newdate.getDate();
        giorni.push(new Date(today.getFullYear(),today.getMonth()+1,dt+1));
        newdate = new Date(today.getFullYear(),today.getMonth(),dt+1);

      }else{

        giorni.push(new Date(today.setDate(date+i)));
      }
    }

    currdate = date+i;

   }
}
var Dates = new Date().getWeek();

But in this week it displays wrong dates..(From 30 November to 6 November) enter image description here

Any suggestion? ( this is the plunker -> plnkr.co/edit/48NyxngWNGIlOps1Arew?p=preview)

Fr4ncx
  • 356
  • 6
  • 22

2 Answers2

1

Here is an answer for getting start of the week and other things using momentjs.

    function getWeekFor(dateTime) {
        var days = [];
        var sunday = moment(dateTime).startOf('week');

        for (var i = 1; i < 8; i++) {
            days.push(moment(sunday).add(i, 'days'));
        }

        return days; // returns a list of moment objects
    }

    function shiftWeek(add, dateTime) {
      // this will just increment or decrement the week
      var sunday = moment(dateTime).startOf('week');
      sunday.add(1, 'd');

      if (add) {
        sunday.add(1, 'w'); 
      } else {
        sunday.subtract(1, 'w'); 
      }

      return sunday; // returns a moment object
    }

    function getWeekDate(dateTime) {
        var sunday  = moment(dateTime).startOf('week');

        var monday = sunday.add({day: 1}).clone();

        return 'Week Commencing ' + monday.format('Do'); // a nicely formatted string of the week commencing
    }

    function getStartOfWeek(dateTime) {
        var sunday  = moment(dateTime).startOf('week');

        var monday = sunday.add({day: 1}).clone();

        return monday; // the monday that started the week as a moment object
    }

It is pretty straightforward and self explanatory. It uses the methods startOf and add passing in w as the unit to add. w == week.

And if you don't want a moment object, heres a list of other objects you can get from it:

moment().toDate(); // returns javascript Date
moment().toArray(); // This returns an array that mirrors the parameters from new Date()
moment().toJSON(); // it will be represented as an ISO8601 string, adjusted to UTC.
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
0

This is because you are changing date manually

giorni.push(new Date(today.getFullYear(),today.getMonth()+1,dt+1));

Instead of this try

var today = new Date()
today.setDate(today.getDate()+1);
giorni.push(new Date(today.getTime()));
Rajesh
  • 24,354
  • 5
  • 48
  • 79