@eartharoid/dtf

中文文档
2.0.1发布于 2021/09/28MITTypeScriptCommonJS零依赖近 30 天下载 2012

A simple date and time formatter.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Placeholders
  4. API
  5. Support
  6. Donate
  7. License

dtf

GitHub Workflow Status

Simple and somewhat-localised timestamp formatting.

Note that in some cases, especially if you want to use standard formats and in other locales, it may be better to use the built-in Date or Intl functions.

Installation

Install with npm/pnpm/yarn:

npm i @eartharoid/dtf

Usage

const DTF = require('@eartharoid/dtf');
const dtf = new DTF();

// a new `Date` will be created
const timestamp = dtf.fill('DD/MM/YYYY HH:mm:ss');
console.log(timestamp); // => `13/07/2021 14:44:51`

// custom `Date` and use UTC time
const timestamp = dtf.fill('DD/MM/YYYY HH:mm:ss', new Date(), true);
console.log(timestamp); // => `13/07/2021 13:44:51`

See DTF#fill.

Placeholders

PlaceholderDescriptionExample
YYYYThe full year number2021
YYThe short year number21
MMMMThe full month nameJuly
MMMThe short month nameJul
MMThe zero-padded month number07
MThe non-padded month number7
DDDDThe full day nameTuesday
DDDThe short day nameTue
DDThe zero-padded date number13
D_The date followed by its ordinal suffix13th
DThe non-padded date number13
HHThe zero-padded 24h hour number14
hhThe zero-padded 12h hour number02
hThe non-padded 12h hour number2
mmThe zero-padded minute number18
mThe non-padded minute number18
ssThe zero-padded second number09
sThe non-padded second number9
SSSThe zero-padded millisecond number638
AMPMAM or PMPM

API

new DTF(locale?)

Create a new DTF instance.

  • locale? - the locale of this DTF instance (default is 'en')

DTF#locale

The locale of this DTF instance.

DTF#fill(format?, date?, utc?)

Fill a given or the default timestamp format using the current date or the Date provided.

  • format? - the placeholder string (default is 'HH:mm:ss')
  • date? - the Date object used to fill the placeholders (defaults to a new Date object)
  • utc? - use UTC time? (default is false)

DTF#AMPM(date?, utc?)

Returns 'AM' or 'PM' based on the time

  • date? - the date to use (defaults to a new Date object)
  • utc? - use UTC time? (default is false)

DTF#suffix(number)

Appends st, nd, rd, or th to the given number for an English locale. I don't know what it does for other locales.

  • number - the number to append the ordinal suffix to

DTF#convert24To12(h)

Converts a 24h number to 12h.

  • h - the 24h number

Support

Discord support server

Donate

ko-fi

or sponsor me.

License

MIT license.

© 2021 Isaac Saunders

版本与发布记录

@eartharoid/dtf 共发布 5 个版本,首次发布于 2020/05/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2021/09/285 年前
2.0.02021/07/135 年前
1.0.82020/10/046 年前
1.0.72020/08/076 年前
1.0.62020/05/046 年前

关于 @eartharoid/dtf 的常见问题

@eartharoid/dtf 是什么?

A simple date and time formatter.

如何安装 @eartharoid/dtf?

在项目目录运行 npm install @eartharoid/dtf,或使用 pnpm add / yarn add / bun add。

@eartharoid/dtf 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eartharoid/dtf 支持 ESM 还是 CommonJS?

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

@eartharoid/dtf 可以商用吗?

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

@eartharoid/dtf 的下载量有多少?

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

@eartharoid/dtf 还在维护吗?

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