Time CountDown in angular 2

前端 未结 6 1418
花落未央
花落未央 2020-12-13 01:41

I want to have a date countdown like this:

http://codepen.io/garethdweaver/pen/eNpWBb

but in angular 2, I found this plunkr that adds 1 to a number each 500

6条回答
  •  隐瞒了意图╮
    2020-12-13 01:58

    This is a bit simplified version with hardcoded date and returning four different outputs (days - hours - minutes - seconds) that you can put nicely in four boxes:

    export class HomePage {
    
        // Hardcoded date
        private eventDate: Date = new Date(2018, 9, 22);
    
        private diff: number;
        private countDownResult: number;
        private days: number;
        private hours: number;
        private minutes: number;
        private seconds: number;
    
        constructor(public navCtrl: NavController ) {
    
            Observable.interval(1000).map((x) => {
                    this.diff = Math.floor((this.eventDate.getTime() - new Date().getTime()) / 1000);
                }).subscribe((x) => {           
                    this.days = this.getDays(this.diff);
                    this.hours = this.getHours(this.diff);
                    this.minutes = this.getMinutes(this.diff);
                    this.seconds = this.getSeconds(this.diff);
                });
        }
    
        getDays(t){
            return Math.floor(t / 86400);
        }
    
        getHours(t){
            const days = Math.floor(t / 86400);
            t -= days * 86400;
            const hours = Math.floor(t / 3600) % 24;
            return hours;
        }
    
        getMinutes(t){
            const days = Math.floor(t / 86400);
            t -= days * 86400;
            const hours = Math.floor(t / 3600) % 24;
            t -= hours * 3600;
            const minutes = Math.floor(t / 60) % 60;
            return minutes;
        }
    
        getSeconds(t){
            const days = Math.floor(t / 86400);
            t -= days * 86400;
            const hours = Math.floor(t / 3600) % 24;
            t -= hours * 3600;
            const minutes = Math.floor(t / 60) % 60;
            t -= minutes * 60;
            const seconds = t % 60;
            return seconds;
        }
    
    }
    

提交回复
热议问题