@art-suite/art-core-ts-time

中文文档
0.4.1发布于 2026/06/19MITTypeScriptESM + CJS近 30 天下载 3224

A TypeScript string utility library

原始使用文档
文档目录9 节
  1. Why This Module?
  2. Milliseconds and Seconds
  3. Example Installation and Use (Required)
  4. Functional Overview
  5. Conversion Utilities
  6. Formatting and Presentation
  7. Time Calculations
  8. Time Unit Constants
  9. API Documentation Reference

@art-suite/art-core-ts-time

Seamless, simple utilities for working with dates, times, and durations in JavaScript and TypeScript.

Why This Module?

The Why: Working with dates, times, and durations in JavaScript is notoriously error-prone and inconsistent. Developers often need to convert between numbers, strings, and Date objects, or present time in human-friendly ways, but the standard APIs are clunky and fragmented.

The How: This library provides a unified set of utilities for effortless conversion between standard date/time representations (number, string, Date), as well as easy formatting and presentation (e.g., formatDate, timeAgo, humanDurationString). It also makes it trivial to convert between different units of time (seconds, hours, days, etc.) and to perform common calculations like "start of day" or "first of month".

The What: A comprehensive set of functions for converting, formatting, and presenting dates and times, plus helpers for working with time units and durations.

Milliseconds and Seconds

JavaScript date-time-as-numbers uses milliseconds as the unit. However, often seconds is the more natural (and efficient) unit of choice. This library solves this problem by making a powerful assumption that allows you to use both milliseconds and seconds interchangeably:

  • Date/Time in Seconds: any number < 100000000000 is considered seconds
  • Date/Time in Milliseconds: any number >= 100000000000 is considered milliseconds

This means that the supported date-time ranges for seconds and milliseconds are:

  • Date/Time in Seconds Range: 1970 to 5138 A.D.
  • Date/Time in Milliseconds Range: 1973-03-03T09:46:40.000Z to basically eternity (ChatGPT says 275,760 A.D.)

In other words, by making a small sacrifice - not being able to represent date/time seconds greater than the year 5138 nor milliseconds in the years 1970 to 1973, we gain the ability to seamlessly work with either common numerical representation of date/time without having to think about which one is being used.

Example Installation and Use (Required)

Install with npm:

npm install @art-suite/art-core-ts-time

Basic usage:

import {
  formatDate,
  toMilliseconds,
  toSeconds,
  toDate,
  timeAgo,
  humanDurationString,
  firstOfDay,
  firstOfMonth,
  secondsPer,
} from "@art-suite/art-core-ts-time";

// Convert between types
toMilliseconds("2024-01-01"); // e.g., 1704067200000
toSeconds(new Date()); // e.g., 1704067200
toDate(1704067200000); // Date object

// Format and present
formatDate(new Date(), "yyyy-MM-dd"); // "2024-06-01"
timeAgo(new Date(Date.now() - 60000)); // "1m ago"
humanDurationString(3661); // "1h 1m 1s"

// Time calculations
firstOfDay(new Date()); // Seconds since epoch at start of today (UTC)
firstOfMonth(new Date()); // Seconds since epoch at start of this month (UTC)

// Time unit constants
secondsPer.hour; // 3600
secondsPer.day; // 86400

Functional Overview

Conversion Utilities

  • toMilliseconds(value) — Convert a Date, string, or number to milliseconds since epoch.
  • toSeconds(value) — Convert a Date, string, or number to seconds since epoch.
  • toDate(value) — Convert a string, number, or Date to a Date object.

Formatting and Presentation

  • formatDate(value, format?, utc?) — Format a date as a string, with optional format and UTC support.
  • timeAgo(date, options?) — Human-friendly "time ago" string (e.g., "2h ago", "just now").
  • humanDurationString(seconds, options?) — Format a duration in seconds as a human-readable string (e.g., "1h 2m 3s").
  • niceDateString(date, now?) — Friendly date string (e.g., "today", "yesterday", "June 1").
  • niceFullDateString(date) — Full, readable date string (e.g., "3:45pm June 1, 2024").

Time Calculations

  • firstOfHour(time) — Seconds since epoch at the start of the hour.
  • firstOfDay(time) — Seconds since epoch at the start of the day (UTC).
  • firstOfWeek(time) — Seconds since epoch at the start of the week (UTC, Monday).
  • firstOfMonth(time) — Seconds since epoch at the start of the month (UTC).
  • firstOfYear(time) — Seconds since epoch at the start of the year (UTC).
  • Locale variants: firstOfDayLocale, firstOfWeekLocale, firstOfMonthLocale, firstOfYearLocale.

Time Unit Constants

  • secondsPer — Object mapping time units (e.g., hour, day, month) to their value in seconds.

API Documentation Reference

For detailed information on all exported functions and their parameters, please refer to the TypeScript typings and JSDoc comments within the source code.

版本与发布记录

@art-suite/art-core-ts-time 共发布 22 个版本,首次发布于 2025/05/12;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.4.1latest2026/06/194 个月前
0.4.02026/03/277 个月前
0.3.02025/12/1910 个月前
0.2.12025/12/0511 个月前
0.1.182025/12/0511 个月前
0.1.172025/12/0411 个月前

关于 @art-suite/art-core-ts-time 的常见问题

@art-suite/art-core-ts-time 是什么?

A TypeScript string utility library

如何安装 @art-suite/art-core-ts-time?

在项目目录运行 npm install @art-suite/art-core-ts-time,或使用 pnpm add / yarn add / bun add。

@art-suite/art-core-ts-time 支持 TypeScript 吗?

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

@art-suite/art-core-ts-time 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@art-suite/art-core-ts-time 可以商用吗?

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

@art-suite/art-core-ts-time 的下载量有多少?

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

@art-suite/art-core-ts-time 还在维护吗?

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