javascript - 如何在html中显示从某个日期经过的时间?
问题描述
我将如何在 html 中制作“实时”时间计数器?例如,我显示自 2001 年 1 月 1 日以来经过的月数、天数、小时数、分钟数和秒数,我希望看到它向上滴答。我该怎么做?我不知道 JS 哈哈,但我记得曾经做过同样的事情,但它只使用一个 HTML 文件显示了直到某个日期的时间。我不记得我是怎么做到的。
解决方案
您可以使用以下软件包
// 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"
推荐阅读
- installation - 在 Inno Setup 中检测到安装被中止/取消
- docker - Docker-compose yaml.scanner.ScannerError:此处不允许映射值
- reactjs - 如何在 jest 和酵素中测试输入类型
- javascript - Bootstrap 4:在 foreach 循环中将一个图标与另一个图标交换的最佳方法是什么?
- python - 来自 .max() 的值作为 Python 中的新数据框
- event-handling - 组件根上的单击事件不起作用
- python - tree.xpath 返回空列表
- python - Python什么时候重置线程数?
- python - 有没有办法在 Windows 上设置 mobfs 而不会出现此错误?
- javascript - 可访问性 javascript:使向下箭头键模拟 TAB 键