@financial-times/o-date

中文文档
7.0.1发布于 2025/06/30MITES Module近 30 天下载 2966

JavaScript utilities for formatting and updating dates in the FT style. Also useful for formatting dates relative to the current time.

原始使用文档
文档目录11 节
  1. Usage
  2. Markup
  3. Core Experience
  4. Format Options
  5. Copy Options
  6. JavaScript
  7. o-date#destroy()
  8. Server-side
  9. Migration
  10. Contact
  11. Licence

o-date

o-date formats time elements in the FT style, and supports formatting dates in relative time.

Usage

Check out how to include Origami components in your project to get started with o-date.

Markup

To render a human readable date in the FT format create a time element. Add a standard datetime attribute in the ISO 8601 format. Then add the data-o-component="o-date" attribute with a class o-date.

<time data-o-component="o-date" class="o-date" datetime="2000-06-14T23:00:00.000Z">
</time>

The rendered date will be a relative time or timestamp for a given date, in accordance with FT date formatting conventions E.g. 10 seconds ago, a minute ago, 3 hours ago, yesterday, July 18 2020.

Core Experience

We recommend a human readable date is added to the time element to support a core experience without JavaScript:

<time data-o-component="o-date" class="o-date" datetime="2000-06-14T23:00:00.000Z">
    June 15 2000
</time>

Node.js applications could provide a core experience fallback using the Financial-Times/ft-date-format library.

Format Options

Set the data-o-date-format attribute to customise how the time element is presented:

  • date-only: only show the date, without relative or time information.
  • time-ago-no-seconds: always display a relative time but if the relative time is under a minute display "Less than a minute ago" (e.g. Less than a minute ago, 10 minutes ago, an hour ago, 4 hours ago).
  • time-ago-limit-4-hours: always display a relative time but hide the time element if the datetime is older than 4 hours (e.g. 4 seconds ago, 10 minutes ago, 4 hours ago).
  • time-ago-limit-24-hours: always display a relative time but hide the time element if the datetime is older than 24 hours (e.g. 4 seconds ago, 10 minutes ago, 10 hours ago).
  • today-or-yesterday-or-nothing: display today if the datetime is today, yesterday if it was yesterday, or hide the time element if the datetime is older than yesterday.
  • custom format: it is recommended to use a standard FT format but a custom format may be used e.g. h:mm a.

Note: There used to be time-ago-abbreviated and time-ago-abbreviated-limit-4-hours options which are now both deprecated. These options still work but no longer show an abbreviated date. They will be removed in a future major version.

Text case

The formatted date defaults to all lower case (https://github.com/Financial-Times/ft-date-format/blob/master/index.js), e.g. 'an hour ago.

Set data-o-date-text-case=sentence attribute on the time element to capitalise only the first letter of the text (e.g. 'An hour ago').

Copy Options

By default o-date will replace the contents of the time element with the formatted date. To include extra content alongside the formatted date add an element with the data-o-date-printer attribute. o-date will output the formatted date within the data-o-date-printer element and will not change other child elements.

For example to include "updated at" copy within the time element followed by a relative time:

<time data-o-component="o-date" class="o-date" datetime="2020-07-18T19:01:05.033Z" data-o-date-format="time-ago-limit-4-hours">
    <!-- some arbitrary content -->
    <span>updated at</span>
    <!-- show the time ago here in the printed element -->
    <!-- fallback to the date if o-date JavaScript fails  -->
    <span data-o-date-printer>
        20 July 2020
    </span>
</time>

Render a date multiple times within the same o-date component by including multiple data-o-date-printer elements. Each printer element may have a unique format by adding the data-o-date-format attribute. If data-o-date-format is not set on the printer element the parent data-o-date-format element will be used:

<time data-o-component="o-date" class="o-date" datetime="2020-07-18T19:01:05.033Z" data-o-date-format="date-only">
    <!-- render the date in the "date-only" format here -->
    <!-- (as set on the parent "time" element) -->
    <span data-o-date-printer></span>
    <!-- render the date in the "time-ago-limit-4-hours" format here -->
    <span data-o-date-printer data-o-date-format="time-ago-limit-4-hours"></span>
    <!-- render the date in the custom format "h:mm" here -->
    <span data-o-date-printer data-o-date-format="h:mm"></span>
</time>

Previously a CSS class o-date__printer was used instead of the data-o-date-printer attribute. The class o-date__printer is now deprecated.

JavaScript

Instantiate o-date JavaScript in the same way as other Origami components.

For example call the init method to initialise all o-date instances on the page:

import ODate from '@financial-times/o-date'
ODate.init();

Pass a specific element:

import ODate from '@financial-times/o-date'
const myDateElement = document.querySelector('#my-date');
ODate.init(myDateElement);

Or dispatch the o.DOMContentLoaded event to auto-construct all Origami components on the page, including each o-date element with a data-o-component="o-date" attribute:

import '@financial-times/o-date';
document.addEventListener('DOMContentLoaded', function() {
    document.dispatchEvent(new CustomEvent('o.DOMContentLoaded'));
});

o-date#destroy()

Call on any instances to stop processing date updates and release the item reference.

Server-side

See the npm package @financial-times/ft-date-format for server-side date formatting. Note: It's not recommended to output the 'time ago' server side as it will not be cacheable and will not update in the browser if the user leaves the page open for a prolonged period of time.

Migration

StateMajor VersionLast Minor ReleaseMigration guide
✨ active6N/Amigrate to v6
⚠ maintained5N/Amigrate to v5
⚠ maintained44.2migrate to v4
⚠ maintained33.1migrate to v3
╳ deprecated22.9-
╳ deprecated11.2-

Contact

If you have any questions or comments about this component, or need help using it, please either raise an issue, visit #origami-support or email Origami Support.


Licence

This software is published by the Financial Times under the MIT licence.

版本与发布记录

@financial-times/o-date 共发布 41 个版本,首次发布于 2019/02/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
7.0.1latest2025/06/301.4 年前
7.0.02025/02/201.7 年前
6.1.02024/03/042 年前
6.0.12023/10/273 年前
6.0.02023/03/213 年前
5.4.02022/10/124 年前

其他发布通道:prerelease → 5.0.0-beta.0,tag--v4.2.1 → 4.2.1,tag--v4.3.0 → 4.3.0。

关于 @financial-times/o-date 的常见问题

@financial-times/o-date 是什么?

JavaScript utilities for formatting and updating dates in the FT style. Also useful for formatting dates relative to the current time.

如何安装 @financial-times/o-date?

在项目目录运行 npm install @financial-times/o-date,或使用 pnpm add / yarn add / bun add。

@financial-times/o-date 支持 TypeScript 吗?

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

@financial-times/o-date 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@financial-times/o-date 可以商用吗?

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

@financial-times/o-date 的下载量有多少?

根据 npm 官方下载统计,@financial-times/o-date 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,966 次,日均约 99 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@financial-times/o-date 还在维护吗?

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