# @expo/timeago\.js 中文文档

规范地址：https://npmcn.com/package/%40expo/timeago.js/

版本：1\.0\.0
npm 元数据更新时间：2026-09-23T11:27:17.675Z
中文译文更新时间：2026-11-14T05:27:36.368Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40expo/timeago.js
原文对照：https://npmcn.com/package/%40expo/timeago.js/?lang=original
项目主页：https://github.com/expo/timeago.js#readme
源码仓库：https://github.com/expo/timeago.js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：32.5 kB，36 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：0 个
- 首次发布：2021-09-13T12:38:45.215Z
- 最新版本发布：2021-09-13T12:38:45.410Z
- 近 30 天下载量：7014176（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

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

## 安装

```sh
npm install @expo/timeago.js
```

也可以使用：`pnpm add @expo/timeago.js`、`yarn add @expo/timeago.js`、`bun add @expo/timeago.js`

## README

# 时间ago.js

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

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

[官方网站 (简体中文)](https://timeago.org/)。在此反应版本 : [时间- 反应](https://github.com/hustcc/timeago-react)。这里的 Python 版本 : [时间](https://github.com/hustcc/timeago)。 。 。 .

[![npm Version](https://img.shields.io/npm/v/timeago.js.svg)](https://www.npmjs.com/package/timeago.js) [![unpkg](https://img.shields.io/npm/v/timeago.js?label=cdn)](https://unpkg.com/browse/timeago.js/) [![Build Status](https://github.com/hustcc/timeago.js/workflows/build/badge.svg)](https://github.com/hustcc/timeago.js/actions) [![Coverage Status](https://coveralls.io/repos/github/hustcc/timeago.js/badge.svg?branch=master)](https://coveralls.io/github/hustcc/timeago.js?branch=master) [![Dist gzip](https://img.badgesize.io/https://unpkg.com/timeago.js/dist/timeago.min.js?compression=gzip)](https://unpkg.com/timeago.js/dist/timeago.min.js) [![npm Download](https://img.shields.io/npm/dm/timeago.js.svg)](https://www.npmjs.com/package/timeago.js) [![npm License](https://img.shields.io/npm/l/timeago.js.svg)](https://www.npmjs.com/package/timeago.js)

例如

```plain
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
```

## 使用量

-   安装

```bash
npm install timeago.js
```

-   导入

```ts
import { format, render, cancel, register } from 'timeago.js';
```

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

```html
<script src="dist/timeago.min.js"></script>
```

-   实例

```ts
// format the time with locale
format('2016-06-12', 'en_US');
```

## 光盘N

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

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

## API 密码

下面只有4个API.

-   **格式**

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

```ts
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 代码 :

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

Javascript 代码 :

```ts
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` 包含:

```ts
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`, (中文(简体) ), [这里所有的地方](https://npmcn.com/package/%40expo/timeago.js/src/lang)。 。 。 .

您可使用 `register` API (英语).

```ts
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 (日语)](https://github.com/rmm5t/jquery-timeago) 这是一个很好的和突出的项目,但它取决于j查询.
2.  **语言翻译**: 图书馆需要更多的地方翻译. 你可以这样:

-   打开一个问题来编写本地语言翻译, 或者提交拉动请求 。 怎么样 [语言翻译](https://npmcn.com/package/%40expo/timeago.js/src/lang/)。 。 。 .
-   请 **测试** 由 exec 编辑 `npm test`。如何写测试案例,参见 [本地测试案例](https://npmcn.com/package/%40expo/timeago.js/__tests__/lang/)。 。 。 .

## 自由主义

麻省理工学院@[开始](https://github.com/hustcc)

## 常见问题

### @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 和提交记录判断。

