I’ve published a framework-independent Time-Ago “pipe” built with Stencil. It’s a web component that can be used like:
Aquinas died <time-ago time="7 January 1274"></time-ago>. // in template
Aquinas died about 744 years ago. // output
or (Angular syntax follows)
now = new Date(); // Typescript
You entered this page <time-ago [time]="now" // in template
seconds="true"></time-ago>.
You entered this page less than 5 seconds ago. // output (updates automatically)
The logic leverages the change detection built into Stencil. The refresh timer is literally one line long:
setTimeout(_ => this.refresh(), this.backoff(this.time));
where this.refresh()
sets a new string to output, and then NOTs a boolean that Stencil’s change detection is watching. So whenever the boolean changes, the component re-renders.
As I said with the Star Rating component, please let me know if you try this out in a framework that is not Ionic Angular, so I can post setup instructions (and give you credit). Thank you.