0

How can I get day before continuously? For example, I have this HTML:

<ul>
<li (click)="prevDay()"></li>
</ul>

<div>{{day}}</div>

JS:

dayBefore() {
        let now: any = new Date();
        this.days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        this.day = this.days[now.getDay() - 1];
}

This way I only get the yesterday, but I need it continuously. When I click again on the <div>{{day}}</div> I need it to go the day before the 'yesterday' and so on.. So, Thursday, Wednesday, Tuesday, etc..

EDIT: same for today and tomorrow...

eric.dummy
  • 399
  • 1
  • 8
  • 24

3 Answers3

1

You can try the following:

    <script src="https://code.angularjs.org/2.0.0-alpha.26/angular2.sfx.dev.js"></script>

<script>
    function AppComponent() {
            this.day = '';
        this.decrement = 0;
        this.days =   ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        this.dayBefore = function() {

                    var now = new Date();  
          this.decrement+=1;
          var newDate = this.decrementDays(now, this.decrement);
          this.day = this.days[newDate.getDay()];               
        },
        this.decrementDays = function addDays(date, days)  {
            return new Date(
        date.getFullYear(),
        date.getMonth(),
        date.getDate() - days,
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
        );
        }

    }

    AppComponent.annotations = [
      new angular.ComponentAnnotation({
        selector: 'my-app'
      }),
      new angular.ViewAnnotation({
        template: '<ul>' +
'<li (click)="dayBefore()">aaaaa</li>' +
'</ul>' +
'<div>{{day}}</div>',
        directives: [angular.NgFor]
      })
    ];

    document.addEventListener('DOMContentLoaded', function() {
      angular.bootstrap(AppComponent);
    });

</script>

<my-app></my-app>
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
1

In your controller, initialize only ONCE a new date the days array

private myDate=new Date();
private days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
private day = this.days[this.myDate.getDay()];;

So you have your today date. Then update your date in the methods which change the date.

private dayBefore(){
   this.myDate=this.myDate.addDays(-1);
   this.day= this.days[this.myDate.getDay()];
}

private dayAfter(){
   this.myDate=this.myDate.addDays(1);
   this.day= this.days[this.myDate.getDay()];
}

private restoreToday(){           
   this.myDate=new Date();
   this.day= this.days[this.myDate.getDay()];
}

And, in your html

<ul>
   <li (click)="dayBefore()">Prev</li>
   <li (click)="restoreToday()">Today</li>
   <li (click)="dayAfter()">Next</li>
</ul>

<div>{{day}}</div>
Gianluca Paris
  • 1,391
  • 1
  • 14
  • 25
1

You should be using as below,

  currentPos=5;
  today : string= 'Friday'
  days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

  constructor() {
    this.today=this.days[this.currentPos];
  }
  prev(){
    //this.daysArray.push(this.days[this.currentPos]);
    if(this.currentPos>0){
      this.currentPos--;
      this.today=this.days[this.currentPos];

    }
    else if(this.currentPos==0){
      this.today=this.days[0]
      this.currentPos=6;
    }


  }
}

LIVE DEMO

Aravind
  • 40,391
  • 16
  • 91
  • 110