Search code examples
angulartypescripttimengondestroy

Angular/Typescript - Get timestamp OnDestroy


I'm trying to get a timestamp for every time a page is accessed and another timestamp when the user leaves the page. I get the time Oninit, however 'OnDestroy' I get the same exact time when of when the page was accessed.

entryTime = Date.now();
exitTime = new Date();

ngOnInit(): void {
  console.log(this.entryTime);
  }

ngOnDestroy(): void {
  console.log(this.exitTime);
  }

Solution

  • Call this inside ngDestroy and then console

    ngOnDestroy(): void {
      this.exitTime = new Date();
      console.log(this.exitTime);
      }