'Replace a substring by a rxjs countdowntimer
I am trying to implement a countdowntimer using rxjs in my angular 12 application. In my ts I have :
let timeLeft$ = interval(1000).pipe(
map(x => this.calcTimeDiff(orderCutOffTime)),
shareReplay(1)
);
calcTimeDiff() will return seconds, minutes and hours;
I have to create a string out of this and display that in the HTML. Basically replace an existing word in the string with timeLeft$ which will be a countdowntimer to be shown in the HTML.
Something like : this.orderCutOffMessage = someString.replace('Down', timeLeft$)
https://henrikmassow.medium.com/implement-a-countdown-timer-with-rxjs-in-angular-61600d1af00c
orderCutOffTime = "1645567200000" // Unix timestamp in milliseconds
private calcTimeDiff(cutOffTime: any): timeComponents {
const finalCutOffTime = (cutOffTime).valueOf();
const milliSecondsInASecond = 1000;
const hoursInADay = 24;
const minutesInAnHour = 60;
const secondsInAMinute = 60;
const timeDifference = finalCutOffTime - Date.now();
const hoursToFinalCutOffTime = Math.floor(
(timeDifference /
(milliSecondsInASecond * minutesInAnHour * secondsInAMinute)) %
hoursInADay
);
const minutesToFinalCutOffTime = Math.floor(
(timeDifference / (milliSecondsInASecond * minutesInAnHour)) %
secondsInAMinute
);
const secondsToFinalCutOffTime =
Math.floor(timeDifference / milliSecondsInASecond) % secondsInAMinute;
return { secondsToFinalCutOffTime, minutesToFinalCutOffTime, hoursToFinalCutOffTime };
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
