Q:

How should an empty timestamp variable be declared, in order to be a global var so we can give it a value later?

I want to set a timestamp as a global variable and set it with empty value. I want to use it inside the functions and assign a value to it. How this timestamp var should be declared?
For example, I have 2 buttons which each button triggers a method
<button (click)="startTime()">Starting timestamp</button>

<button (click)="endTime()">Ending timestamp</button>

I want to have two timestamp variables declared as global variables in component.ts , in order to get these dates when button are clicked and then update other methods.

A:

Add the variable inside the class:
export class MyComponent {

  myTimestamp;
  myOtherTimestamp;

  startTime() {
    this.myTimestamp = new Date().getTime();
  }

  endTime() {
    this.myOtherTimestamp = new Date().getTime();
  }
}

For sharing with other components / services, you can add it to a singleton service and access it wherever you want:
@Injectable({
  providedIn: 'root',
})
export class MySharedService {
  myTimestamp;
}

and access it by injecting the service:
export class MyComponent {
  constructor(private mySharedService: MySharedService ){}

  myCustomFunction() {
   this.mySharedService.myTimestamp = new Date().getTime();
  }
}

