dairy
dairy

Reputation: 181

how do i show the amount of time elapsed from a certain date in html?

How would I make a "live" time counter in html? For example, I show the number of months, days, hours, minutes, and seconds that have passed since 1/1/2001 and I want to see it ticking upwards. How would I do that? I don't know JS lol, but I do remember once making the same thing but it displayed the time until a certain date using just one HTML file. I don't recall how I did it though.

Upvotes: 1

Views: 952

Answers (2)

Ravi Ranjan
Ravi Ranjan

Reputation: 84

You can use the below package

javascript-time-ago

// Load locale-specific relative date/time formatting rules.
import en from 'javascript-time-ago/locale/en'
 
// Add locale-specific relative date/time formatting rules.
TimeAgo.addLocale(en)
 
// Create relative date/time formatter.
const timeAgo = new TimeAgo('en-US')
 
timeAgo.format(new Date())
// "just now"
 
timeAgo.format(Date.now() - 60 * 1000)
// "a minute ago"
 
timeAgo.format(Date.now() - 2 * 60 * 60 * 1000)
// "2 hours ago"
 
timeAgo.format(Date.now() - 24 * 60 * 60 * 1000)
// "a day ago"

Upvotes: 2

user14772095
user14772095

Reputation:

If you want one, go to this site. You can make a custom one with them. If you want to write one from scratch, go to their GitHub page and tune it to your liking.

Upvotes: 1

Related Questions