Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@cirolee/relative-time

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cirolee/relative-time

A simple library to format dates relative to the current time.

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

relative-time

一个简单的库,用于将日期格式化为相对于当前时间的相对时间字符串。

功能

  • 支持多种语言环境
  • 使用 Intl.RelativeTimeFormat 实现
  • 自动缓存 RelativeTimeFormat 实例以提高性能
  • 支持传入字符串、数字或 Date 对象作为日期参数

安装

npm install @cirolee/relative-time

使用

import relativeTime from '@cirolee/relative-time';

// 基本使用
console.log(relativeTime('2025-09-20')); // "1天后"
console.log(relativeTime('2025-09-18')); // "1天前"
console.log(relativeTime(new Date())); // "现在"

// 指定语言环境
console.log(relativeTime('2025-09-20', 'en-US')); // "in 1 day"
console.log(relativeTime('2025-09-18', 'en-US')); // "1 day ago"

API

relativeTime(date, locale?)

将给定日期格式化为相对于当前时间的相对时间字符串。

参数

  • date (string | number | Date): 要格式化的日期
  • locale (Intl.LocalesArgument, 可选): 语言环境,默认为 'zh-Hans-CN'

返回值

(string): 格式化后的相对时间字符串

许可证

MIT

Keywords

relative time

FAQs

Package last updated on 19 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts