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 };
}
CodePudding user response:
Here's a working example of your code https://stackblitz.com/edit/angular-material-table-data-source-znbvdb. It's basicly almost like the one in the link you shared, except that the names of the vars have been changed, one var discarded (days) and everything put in one .ts file. Please let me know if that's what you are looking for. Your orderCutOffTime
has been hard-coded, but you are probably getting it in some subscription so you can tweak it. Also, timeString
propably comes from subscription too.