@expo/timeago.js

中文文档
1.0.0发布于 2021/09/13MITCommonJS零依赖近 30 天下载 701.4万

timago.js是一个简单的库(只有1kb),用于格式化日期时间 *** time ago 语句。 如:"3小时前",支持本地化.

中文使用文档
文档目录5 节
  1. 使用量
  2. 光盘N
  3. API 密码
  4. 捐款
  5. 自由主义

时间ago.js

时间ago.js 是纳米库(小于 2 kb)用于格式化日期时间 *** time ago 语句。 如:"三小时前".

  • i18n 支持过.
  • 时间 ago 时间 in 已支持.
  • 实时渲染支持 .
  • 节点和浏览器支持 .
  • 测试的不错.

官方网站 (简体中文)。在此反应版本 : 时间- 反应。这里的 Python 版本 : 时间。 。 。 .

npm Version unpkg Build Status Coverage Status Dist gzip npm Download npm License

例如

just now
12 seconds ago
2 hours ago
3 days ago
3 weeks ago
2 years ago

in 12 seconds
in 3 minutes
in 24 days
in 6 months

使用量

  • 安装
npm install timeago.js
  • 导入
import { format, render, cancel, register } from 'timeago.js';

导入 script 在 html 文件内标记并访问全局变量 timeago。 。 。 .

<script src="dist/timeago.min.js"></script>
  • 实例
// format the time with locale
format('2016-06-12', 'en_US');

光盘N

作为国家预防机制的替代,您也可以使用CDN,它一发布至npm,就反映最新版本.

<script src="//unpkg.com/timeago.js"></script>

API 密码

下面只有4个API.

  • 格式

format(date[, locale = 'en_US', opts]),格式化为日期实例/时间戳/日期字符串.

import { format } from 'timeago.js';

// format timestamp
format(1544666010224);

// format date instance
format(new Date(1544666010224));

// format date string
format('2018-12-12');

// format with locale
format(1544666010224, 'zh_CN');

// format with locale and relative date
format(1544666010224, 'zh_CN', { relativeDate: '2018-11-11' });

// e.g.
format(Date.now() - 11 * 1000 * 60 * 60); // returns '11 hours ago'

默认语言是 en_US,且该图书馆包含 en_US 财务报告和审定财务报表 zh_CN 内建.

  • 提交 打开 取消( e)

render(dom[, locale = 'en_US', opts])
cancel([dom])

做一个多姆 datetime 属性自动渲染并取消.

HTML 代码 :

<div class="timeago" datetime="2016-06-30 09:20:00"></div>

Javascript 代码 :

import { render, cancel } from 'timeago.js';

const nodes = document.querySelectorAll('.timeago');

// use render method to render nodes in real time
render(nodes, 'zh_CN');

// render with opts
// render(nodes, 'en_US', { minInterval: 3 });

// cancel all real-time render task
cancel();

// or cancel for the specific one
cancel(nodes[0])

第三参数 opts 包含:

export type Opts = {
  /** the relative date */
  readonly relativeDate?: TDate;
  /** the realtime min update interval */
  readonly minInterval?: number;
};

DOM 对象应该有属性 datetime 带有日期格式化字符串.

  • 注册

register(locale, localeFunc),注册一个新的地方,内建的地方包括: en_US, (中文(简体) ), zh_CN, (中文(简体) ), 这里所有的地方。 。 。 .

您可使用 register API (英语).

const localeFunc = (number: number, index: number, totalSec: number): [string, string] => {
  // number: the timeago / timein number;
  // index: the index of array below;
  // totalSec: total seconds between date to be formatted and today's date;
  return [
    ['just now', 'right now'],
    ['%s seconds ago', 'in %s seconds'],
    ['1 minute ago', 'in 1 minute'],
    ['%s minutes ago', 'in %s minutes'],
    ['1 hour ago', 'in 1 hour'],
    ['%s hours ago', 'in %s hours'],
    ['1 day ago', 'in 1 day'],
    ['%s days ago', 'in %s days'],
    ['1 week ago', 'in 1 week'],
    ['%s weeks ago', 'in %s weeks'],
    ['1 month ago', 'in 1 month'],
    ['%s months ago', 'in %s months'],
    ['1 year ago', 'in 1 year'],
    ['%s years ago', 'in %s years']
  ][index];
};
// register your locale with timeago
register('my-locale', localeFunc);

// use it
format('2016-06-12', 'my-locale');

捐款

  1. 网站基于 rmm5t/jquery-timeago (日语) 这是一个很好的和突出的项目,但它取决于j查询.
  2. 语言翻译: 图书馆需要更多的地方翻译. 你可以这样:
  • 打开一个问题来编写本地语言翻译, 或者提交拉动请求 。 怎么样 语言翻译。 。 。 .
  • 请 测试 由 exec 编辑 npm test。如何写测试案例,参见 本地测试案例。 。 。 .

自由主义

麻省理工学院@开始

版本与发布记录

@expo/timeago.js 共发布 1 个版本,首次发布于 2021/09/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2021/09/135 年前

关于 @expo/timeago.js 的常见问题

@expo/timeago.js 是什么?

timago.js是一个简单的库(只有1kb),用于格式化日期时间 *** time ago 语句。 如:"3小时前",支持本地化.

如何安装 @expo/timeago.js?

在项目目录运行 npm install @expo/timeago.js,或使用 pnpm add / yarn add / bun add。

@expo/timeago.js 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/expo__timeago.js 是否存在。

@expo/timeago.js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@expo/timeago.js 可以商用吗?

@expo/timeago.js 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@expo/timeago.js 的下载量有多少?

根据 npm 官方下载统计,@expo/timeago.js 在 2026/10/14 – 2026/11/12 这 30 天内被下载 7,014,176 次,日均约 233,806 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@expo/timeago.js 还在维护吗?

最新版本 1.0.0 发布于 2021/09/13(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/09/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。