-1

** This is My Typescript file **

        import { Component, OnInit } from '@angular/core';
        import { interval, Subscription } from 'rxjs';

        @Component({
           selector: 'app-timer-app',
           templateUrl: './timer-app.component.html',
           styleUrls: ['./timer-app.component.css']
       })
       export class TimerAppComponent implements OnInit {

       model = new Date() //'2021 08 12  00:00:00'
       constructor() { }

       private timeinterval = interval
       private subscription!: Subscription;

       public dateNow = new Date();
       public dDay = this.model.getTime();
       milliSecondsInASecond = 1000;
       hoursInADay = 24;
       minutesInAnHour = 60;
       SecondsInAMinute  = 60;

      public timeDifference: any;
      public secondsToDday:any;
      public minutesToDday:any;
      public hoursToDday:any;
      public daysToDday:any;


      private getTimeDifference () {
          this.timeDifference = this.dDay - new  Date().getTime();
          this.allocateTimeUnits(this.timeDifference);
     }

     private allocateTimeUnits (timeDifference: any) {
        this.secondsToDday = Math.floor((timeDifference) / 
        (this.milliSecondsInASecond) 
         % this.SecondsInAMinute);
         this.minutesToDday = Math.floor((timeDifference) / 
       (this.milliSecondsInASecond * 
        this.minutesInAnHour) % this.SecondsInAMinute);
       this.hoursToDday = Math.floor((timeDifference) / (this.milliSecondsInASecond * 
        this.minutesInAnHour * this.SecondsInAMinute) % this.hoursInADay);
        this.daysToDday = Math.floor((timeDifference) / (this.milliSecondsInASecond * 
        this.minutesInAnHour * this.SecondsInAMinute * this.hoursInADay));
   }

    ngOnInit() {
        this.stratInterval();
   }

    ngOnDestroy() {
        this.subscription.unsubscribe();
   }
   stopInterval(){
       this.subscription.unsubscribe();
  }
    stratInterval(){
       this.subscription = this.timeinterval(1000)
      .subscribe(x => { this.getTimeDifference(); });
  }

}

  • When I run the app then by default date (current date) is loaded and shown and when i select the other date i could not change in my count down timer html file it still defulat value of date picker but i want to select the date and start count down of the date from the selected date to current date *

** This is My Html File **

  <div class="container">
       <h1 class="text-success">Date Picker App</h1>
        <form class="form-inline">
            <div class="form-group">
                <div class="input-group">
                    <input class="form-control" placeholder="yyyy-mm-dd"
                       name="dp" [(ngModel)]="model" ngbDatepicker 
                        #d="ngbDatepicker">
                     <div class="input-group-append">
                        <button class="btn btn-outline-secondary calendar" 
                       (click)="d.toggle()" type="button"></button>
                     </div>
               </div>
           </div>
      </form>   
 </div>

 <!--Count Down Timer DIsplay -->

   <div class="container mt-5">
  
       <div class="timer" *ngIf="model">
           <h2>Time Left</h2>
           <span id="days"> {{daysToDday}} </span> Day(s)
           <span id="hours"> {{hoursToDday}} </span>Hrs 
           <span id="minutes"> {{minutesToDday}} </span>Min
           <span id="seconds"> {{secondsToDday}} </span>S <br>
           <div class="mt-3">
               <button  (click)="stopInterval()" class="btn btn-danger btn- 
               sm">Stop</button>
               <button  (click)="stratInterval()" class="btn btn-info btn-sm 
               "id="resetbtn">Restart</button>
          </div>
     </div>
 </div>

** I Have Tried Many Options and Logics but Its not Working Please Anyone can Guide me How I can Implement This **

** This is the Screenshot of Result ** enter image description here

Amir Shahzad
  • 182
  • 2
  • 10

1 Answers1

1

The problem is that your getTimeDifference function calculated the time difference based on dDay:

this.timeDifference = this.dDay - new Date().getTime();

but dDay is only ever set once (where the variable is declared):

public dDay = this.model.getTime();

When a new date is chosen in the date picker, the variable model is updated but dDay is never updated and it's value is the original date. If you add the following line to your getTimeDifference function:

this.dDay = this.model.getTime();

then dDay will get updated with the new date after one is selected. The getTimeDifference function becomes:

private getTimeDifference() {
  this.dDay = this.model.getTime(); // new line to update dDay
  this.timeDifference = this.dDay - new Date().getTime();
  this.allocateTimeUnits(this.timeDifference);
}

Please see this StackBlitz for a demo. Now when you select a new date, the countdown timer gets updated accordingly.

Ian A
  • 5,622
  • 2
  • 22
  • 31
  • Hello sir i am facing issue in other project with same logic with different input field please can you help me this is the link of my code https://stackblitz.com/edit/angular-ng-bootstrap-demo-vbqcpp?file=app%2Fapp.component.ts here i want to create count down selecting the time from the ngb-timepicker ......thanks in advance – Amir Shahzad Aug 21 '21 at 17:29
  • Could you explain a bit more about what you're trying to do here please? You've just got a time picker, so are you assuming that the time selected is on the current day or the next day? – Ian A Aug 23 '21 at 11:22
  • i want to create a count down timer on the basis of selecting time from the time picker and then start count down from the selecting time from time picker to current time from the current date – Amir Shahzad Aug 24 '21 at 03:11
  • when user select time from the time picker then the count down start – Amir Shahzad Aug 24 '21 at 03:13
  • Like this? https://stackblitz.com/edit/angular-ng-bootstrap-demo-ywtuaz?file=app%2Fapp.component.ts – Ian A Aug 24 '21 at 07:53
  • yes sir but only one issue is in this code when i stop it then counting is stop but the in the backend of the code counting still running when i restart it again then counting is not starting from the stage where i stop the counting and also counting should be start from the given by default values that are given in the time variable please could you solve it sir – Amir Shahzad Aug 24 '21 at 09:33
  • in your solution when i give the value down the 14 hours then it counting in the negative form – Amir Shahzad Aug 24 '21 at 09:38
  • sir my main issue is i want to create a count down which start counting when user give the value in the time picker fields in hours minute and seconds and if user not give the values then it start counting with default given values in the time variable .. i just want to create a count down like stop watch – Amir Shahzad Aug 24 '21 at 09:40
  • 1
    Is this any closer to what you want? https://stackblitz.com/edit/angular-ng-bootstrap-demo-sh7jyw?file=app%2Fapp.component.ts – Ian A Aug 24 '21 at 12:29
  • dear sir everything is fine but when i stop and then again restart it then its not restart from the previous state where i stop it and also when i change value and its start counting but its does not stop. every thing is well but only one functionality is not working well – Amir Shahzad Aug 24 '21 at 13:19
  • its start from the starting point when i resume it please can you solve this im very thankful to you sir – Amir Shahzad Aug 24 '21 at 13:21
  • 1
    You could try this: https://stackblitz.com/edit/angular-ng-bootstrap-demo-cuxt7p?file=app%2Fapp.component.ts This now resumes when you stop and restart. Also if you change the time in the `ngb-timepicker` then press "Restart", it will start counting down based on the new time – Ian A Aug 25 '21 at 12:53
  • sir why its not stop when i change the value from the ngb-timepicker after count down its not stop – Amir Shahzad Aug 26 '21 at 02:05
  • 1
    https://stackblitz.com/edit/angular-ng-bootstrap-demo-cuxt7p?file=app%2Fapp.component.ts The countdown now stops when you change the time value using the `ngb-timepicker`. If you press the "Restart" button the timer will start from the new value selected. – Ian A Aug 26 '21 at 08:00
  • please can you solve this problem i'm stuck in this from many days please https://stackoverflow.com/questions/69760619/how-to-seperate-sender-and-receiver-message-on-basis-of-their-socket-ids-in-angu – Amir Shahzad Nov 07 '21 at 13:47