component
import { Component, OnInit, Input } from '@angular/core';
import { Observable, interval } from 'rxjs';
import {takeWhile, map, tap } from 'rxjs/operators';
@Component({
selector: 'app-count-down',
templateUrl: './count-down.component.html',
styleUrls: ['./count-down.component.css']
})
export class CountDownComponent implements OnInit {
@Input() startDate :Date;
@Input() endDate : Date;
private _MS_PER_SECOND = 1000;
countDown$: Observable<string>;
constructor() { }
ngOnInit() {
this.countDown$ = this.getCountDownObservable(this.startDate , this.endDate)
}
private getCountDownObservable(startDate:Date,endDate:Date){
return interval(1000).pipe(
map(elapse => this.diffInSec(startDate,endDate) - elapse),
takeWhile(gap => gap >= 0),
map(sec => ({
day:Math.floor(sec / 3600 /24),
hour