@tiny-libs/time v1.0.0
@tiny-libs/time
Tiny date/time formatting library. Inspired by Day.js and Moment.js.
Features
- No chaining
 - Only practical APIs
 - Minimal and fast
 - Support Node.js and browser
 - ESM support
 
Install
npm i @tiny-libs/timeUsage
import time from '@tiny-libs/time'
time.format(new Date(), 'YYYY/MM/DD HH:mm')APIs
format
Format date/time.
Type Signature:
function format(date: Date, formatStr?: string): stringExample Usage:
time.format(new Date(2024, 3, 19), 'YYYY-MM-DD') // 2024-03-19List of formats
Format Output Description YY24 Two-digit year YYYY2024 Four-digit year M1-12 The month, beginning at 1 MM01-12 The month, 2-digits MMMJan-Dec The abbreviated month name MMMMJanuary-December The full month name D1-31 The day of the month DD01-31 The day of the month, 2-digits d0-6 The day of the week, with Sunday as 0 ddSu-Sa The min name of the day of the week dddSun-Sat The short name of the day of the week ddddSunday-Saturday The name of the day of the week H0-23 The hour HH00-23 The hour, 2-digits h1-12 The hour, 12-hour clock hh01-12 The hour, 12-hour clock, 2-digits m0-59 The minute mm00-59 The minute, 2-digits s0-59 The second ss00-59 The second, 2-digits SSS000-999 The millisecond, 3-digits Z+05:00 The offset from UTC, ±HH:mm ZZ+0500 The offset from UTC, ±HHmm AAM PM aam pm 
add
Clone a date object with a specified amount of time added.
Type Signature:
function add(date: Date, num: number, unit: TimeUnit): DateTime Unit:
second,minute,hour,day,week,month,yearExample Usage:
time.add(new Date(), -1, 'day')
startOf
Clone a date object and set it to the start of the time unit.
Type Signature:
function startOf(date: Date, unit?: TimeUnit): DateExample Usage:
time.startOf(new Date(), 'day')
endOf
Clone a date object and set it to the end of the time unit.
Type Signature:
function endOf(date: Date, unit?: TimeUnit): DateExample Usage:
time.endOf(new Date(), 'day')
isSame
Compares two dates for equality in specified exact units.
Type Signature:
function isSame(date1: Date, date2: Date, unit?: TimeUnit): booleanExample Usage:
const date = time.add(new Date(), -1, 'minute') time.isSame(date, new Date(), 'hour') // true
clone
Clone for specified date object.
Type Signature:
function clone(date: Date | number): Date
from
Returns the string of relative time from now.
Type Signature:
function fromNow(date: number | Date, withoutAffix?: boolean, allowNow?: boolean): stringExample Usage:
const d1 = time.add(new Date(), -1, 'minute') const d2 = time.add(new Date(), 1, 'hour') time.fromNow(d1) // 1 minute ago time.fromNow(d2) // in 1 hourThe base strings are localized by the current locale and can be customized with the
relativeTimelocale object.
socialize
Returns the social style date/time string of relative time from now.
Type Signature:
function socialize(date: Date | number, displayHourMinute?: boolean): stringExample Usage:
const d1 = new Date() const d2 = time.add(new Date(), 1, 'day') const d3 = time.add(new Date(), 2, 'day') time.socialize(d1) // 22:00 time.socialize(d2) // Yesterday time.socialize(d3) // Monday 22:00Range Key Sample Output Today LT 22:00 Yesterday Yesterday | Yesterday LT Yesterday 22:00 From this week to the day before yesterday LW | LWT wed 22:00 This year LDM | LDMT 11/12 22:00 Before this year LDMY | LDMYT 11/12/2024 22:00 These strings are localized, and can be customized with the
formatslocale object.
I18n
Built-in en-US and zh-CN locales. You can customize the locale by following other locale object templates.
import time from '@tiny-libs/time'
import zh from '@tiny-libs/time/zh'
time.locale('zh-CN', zh)License
MIT copyright © 2024-present alex wei
2 years ago