GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - eastcoastdeveloper/angular-countdown-timer-component: Angular Countdown Timer

BranchesTags
Open more actions menu

Latest commit

 

History

31 Commits
31 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
README.md
README.md
 
 
angular.json
angular.json
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

Angular Countdown Timer Comonent

Angular Countdown Timer — Technical Documentation Overview

This document explains the architecture and implementation details of the Angular Countdown Timer component, including:

Template structure

DOM access via ViewChild

Time calculations

Update loop and lifecycle timing

Data flow between component logic and the DOM

<div class="count-down-timer">
  <p>Countdown to {{ currentTime }}</p>

  <div class="wrapper">
    <div class="description">
      <p>Days</p>
      <p>Hours</p>
      <p>Minutes</p>
      <p>Seconds</p>
    </div>

    <div class="times">
      <p #days></p>
      <p #hours></p>
      <p #minutes></p>
      <p #seconds></p>
    </div>
  </div>
</div>

Key Points

{{ currentTime }} displays the human-readable target date.

Template reference variables (#days, #hours, etc.) enable direct DOM manipulation using @ViewChild.

  1. Component Logic (app.component.ts):
import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})

export class AppComponent implements AfterViewInit {
  date: any;
  now: any;
  targetDate: any = new Date(2026, 11, 31);
  targetTime: any = this.targetDate.getTime();
  difference: number;

  months: Array<string> = [
    'January','February','March','April','May','June',
    'July','August','September','October','November','December',
  ];

  currentTime: any = `${
    this.months[this.targetDate.getMonth()]
  } ${this.targetDate.getDate()}, ${this.targetDate.getFullYear()}`;

  @ViewChild('days', { static: true }) days: ElementRef;
  @ViewChild('hours', { static: true }) hours: ElementRef;
  @ViewChild('minutes', { static: true }) minutes: ElementRef;
  @ViewChild('seconds', { static: true }) seconds: ElementRef;

  ngAfterViewInit() {
    setInterval(() => {
      this.tickTock();
      this.difference = this.targetTime - this.now;
      this.difference = this.difference / (1000 * 60 * 60 * 24);

      !isNaN(this.days.nativeElement.innerText)
        ? (this.days.nativeElement.innerText = Math.floor(this.difference))
        : (this.days.nativeElement.innerHTML = `<img src="https://i.gifer.com/VAyR.gif" />`);
    }, 1000);
  }

  tickTock() {
    this.date = new Date();
    this.now = this.date.getTime();

    this.days.nativeElement.innerText = Math.floor(this.difference);
    this.hours.nativeElement.innerText = 23 - this.date.getHours();
    this.minutes.nativeElement.innerText = 60 - this.date.getMinutes();
    this.seconds.nativeElement.innerText = 60 - this.date.getSeconds();
  }
}
  1. Target Date and Display Formatting Target Date Initialization
targetDate = new Date(2026, 11, 31);
targetTime = this.targetDate.getTime();

JavaScript months are 0-indexed (11 = December).

targetTime stores the timestamp in milliseconds.

Human-Readable Date

currentTime = "December 31, 2026";

This value is rendered in the template.

  1. DOM Access Using @ViewChild
@ViewChild('days') days: ElementRef;
@ViewChild('hours') hours: ElementRef;
@ViewChild('minutes') minutes: ElementRef;
@ViewChild('seconds') seconds: ElementRef;
Template Ref DOM Updates Purpose
#days innerText Shows remaining days
#hours innerText Hours left today
#minutes innerText Minutes left this hour
#seconds innerText Seconds left this minute
  1. Update Cycle (ngAfterViewInit())

Executed after the view initializes:

setInterval(() => {
  this.tickTock();
  this.difference = (this.targetTime - this.now) / (1000 * 60 * 60 * 24);
  this.days.nativeElement.innerText = Math.floor(this.difference);
}, 1000);

Calls tickTock() every second.

Computes total days remaining.

Updates the DOM with new values.

  1. Time Calculation Logic (tickTock())
tickTock() {
  this.date = new Date();
  this.now = this.date.getTime();

  this.days.nativeElement.innerText = Math.floor(this.difference);
  this.hours.nativeElement.innerText = 23 - this.date.getHours();
  this.minutes.nativeElement.innerText = 60 - this.date.getMinutes();
  this.seconds.nativeElement.innerText = 60 - this.date.getSeconds();
}

Breakdown

Unit Calculation Explanation
Days Math.floor(difference) Derived from total milliseconds difference
Hours 23 - currentHour Remaining hours today
Minutes 60 - currentMinutes Remaining minutes this hour
Seconds 60 - currentSeconds Remaining seconds this minute
  1. Summary

The countdown timer calculates the difference between the current time and a future target date.

It updates DOM values every second via setInterval inside ngAfterViewInit().

Uses @ViewChild to directly modify DOM elements.

Displays days, hours, minutes, and seconds remaining with a clean UI.

About

Angular Countdown Timer

stackblitz.com/edit/angular-countdown-timer-component

Topics

angularangular-appangular-componentangular-componentsangular-countdownangular-sample-appangular-timercountdown-timercountdown-timerstimer

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.