1.0.0 • Published 1 year ago

@tiny-libs/time v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@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/time

Usage

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): string
  • Example Usage:

    time.format(new Date(2024, 3, 19), 'YYYY-MM-DD') // 2024-03-19

    List of formats

    FormatOutputDescription
    YY24Two-digit year
    YYYY2024Four-digit year
    M1-12The month, beginning at 1
    MM01-12The month, 2-digits
    MMMJan-DecThe abbreviated month name
    MMMMJanuary-DecemberThe full month name
    D1-31The day of the month
    DD01-31The day of the month, 2-digits
    d0-6The day of the week, with Sunday as 0
    ddSu-SaThe min name of the day of the week
    dddSun-SatThe short name of the day of the week
    ddddSunday-SaturdayThe name of the day of the week
    H0-23The hour
    HH00-23The hour, 2-digits
    h1-12The hour, 12-hour clock
    hh01-12The hour, 12-hour clock, 2-digits
    m0-59The minute
    mm00-59The minute, 2-digits
    s0-59The second
    ss00-59The second, 2-digits
    SSS000-999The millisecond, 3-digits
    Z+05:00The offset from UTC, ±HH:mm
    ZZ+0500The 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): Date

    Time Unit: second, minute, hour, day, week, month, year

  • Example 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): Date
  • Example 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): Date
  • Example 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): boolean
  • Example 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): string
  • Example 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 hour

    The base strings are localized by the current locale and can be customized with the relativeTime locale object.

socialize

Returns the social style date/time string of relative time from now.

  • Type Signature:

    function socialize(date: Date | number, displayHourMinute?: boolean): string
  • Example 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:00
    RangeKeySample Output
    TodayLT22:00
    YesterdayYesterday | Yesterday LTYesterday 22:00
    From this week to the day before yesterdayLW | LWTwed 22:00
    This yearLDM | LDMT11/12 22:00
    Before this yearLDMY | LDMYT11/12/2024 22:00

    These strings are localized, and can be customized with the formats locale 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

1.0.0

1 year ago