export class CountdownTimer {
// Set the end date for the countdown
private targetDate: Date = new Date(2018, 9, 22);
private timeDifference: number;
private countdownValue: number;
private daysLeft: number;
private hoursLeft: number;
private minutesLeft: number;
private secondsLeft: number;
constructor() {
Observable.interval(1000).map((x) => {
this.timeDifference = Math.floor((this.targetDate.getTime() - new Date().getTime()) / 1000);
}).subscribe((x) => {
this.daysLeft = this.calculateDays(this.timeDifference);
this.hoursLeft = this.calculateHours(this.timeDifference);
this.minutesLeft = this.calculateMinutes(this.timeDifference);
this.secondsLeft = this.calculateSeconds(this.timeDifference);
});
}
calculateDays(t){
var days;
days = Math.floor(t / 86400);
return days;
}
calculateHours(t){
var days, hours;
days = Math.floor(t / 86400);
t -= days * 86400;
hours = Math.floor(t / 3600) % 24;
return hours;
}
calculateMinutes(t){
var days, hours, minutes;
days = Math.floor(t / 86400);
t -= days * 86400;
hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
minutes = Math.floor(t / 60) % 60;
return minutes;
}
calculateSeconds(t){
var days, hours, minutes, seconds;
days = Math.floor(t / 86400);
t -= days * 86400;
hours = Math.floor(t / 3600) % 24;
t -= hours * 3600;
minutes = Math.floor(t / 60) % 60;
t -= minutes * 60;
seconds = t % 60;
return seconds;
}