Implement a Countdown Timer with RxJS in Angular
Implement a simple countdown in Angular
Counting down to some special event is something we do a lot.
In this guide, I will show you how to implement a simple and effective countdown timer in Angular using RxJS.
So far, 2020 has been a roller coaster year. How about we implement a countdown timer to the year 2021? The countdown timer we will implement will be updated to the second.
Without further ado, let’s jump into the code.
Create an Angular application
We will get started by creating a simple Angular application via the CLI with the following command.
$ ng new count-down-timer --defaultsOnce the application is created go into its root directory.
$ cd count-down-timerThen add a new component named count-down which will contain the logic for our countdown timer using the following CLI command.
ng g c count-down
At this stage, your project structure should be similar to this.

Implementing the countdown logic
The logic for the countdown time is not complicated. We need to find the difference between the D-day i.e., the day of our event and the current time.
The difference in the time we get is in Milliseconds, so we will convert it into corresponding units i.e., days, hours, minutes, and seconds.
To make sure that the time keeps updating every second, we will use subscription interval which will get the difference in time every second.
To avoid memory leaks in the application, we will unsubscribe from the subscription whenever the count-down component is being destroyed.
The component code should look as below. I have gone for readability over efficiency, so feel free to make modifications if you wish to.
I will not go into detail regarding the time difference calculations in this article, however, I have provided a link to an article regarding the same in the conclusion section.







