Creating a timestamp in Typescript | Quickies in Angular
Frequently, I found myself needing a time (current date), a simple solution. Interesting enough, there is no simple solution in JavaScript. Here goes my simple solution.
Install the dependence
This is a normal NPM package, as any package, just use NPM.
npm i ideacodinglab
You can check your package.json file, it should be there! 😎🤗
Using the date at a Typescript file
Using the date at a HTML file
The HTML is bound to the TS file, thus, you need to make sure it starts before the HTML (e.g., put the method calling at the ngOnInit method, or constructor).
Sandbox
You can find the live app here.
Comments
The output is a string, if you need a date format, it may be a problem. I have passed using an interface in Typescript set as date, and it worked just fine.