I need to calculate the time difference between 2 dates and display it. If the difference is greater than a year, show the number of years only. If it's more than a day, show the number of days. If it's less than a day, show the number of hours. If it's less than an hour, display "You have less than one hour left."
One possible solution is to utilize Moment.js:
remainingTime() {
moment.locale('fr_FR');
return moment(new Date(this.finishedAt)).from(new Date(this.startedAt));
}
However, this method might not give the exact result if the remaining time is less than a day.
How can we achieve accurate results using native JavaScript or Moment.js for calculating the time difference?