首页 > 解决方案 > 如何在html中显示从某个日期经过的时间?

问题描述

我将如何在 html 中制作“实时”时间计数器?例如,我显示自 2001 年 1 月 1 日以来经过的月数、天数、小时数、分钟数和秒数,我希望看到它向上滴答。我该怎么做?我不知道 JS 哈哈,但我记得曾经做过同样的事情,但它只使用一个 HTML 文件显示了直到某个日期的时间。我不记得我是怎么做到的。

标签: javascripthtml

解决方案


您可以使用以下软件包

javascript时间前

// 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"

推荐阅读