2.0.0 • Published 1 year ago

small-date v2.0.0

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

small-date

A tiny (0.8kb) date formatting library with built-in i18n support.

Why?

There are several date formatting libraries out there. It all started with Moment, while nowadays smaller alternatives such as date-fns, DayJS and Luxon among others are raising in popularity. While date-fns already comes in a really small format, it was still way to big for my most common use case: Format a date with a given pattern. So I created this tiny library as an attempt to solve this specific problem with as little bytes as possible.

How it works

I was using Intl.DateTimeFormat previously and really liked it. So I had the idea to use that under the hood while providing a nice API that accepts a pattern in a widely used format. The nice thing about Intl.DateTimeFormat is that it has built-in i18n support.

Browser Support

It works in all modern browsers.

However, if you need to support IE11, pick one of the libraries mentioned above!

Installation

yarn add small-date

Usage

The libraries exports a single function that accepts the following parameters:

ParameterTypeDescription
DateDateThe date that is formatted
PatternstringThe pattern in which the date should be formatted
ConfigObjectA config option that accepts a locale and a timeZone
import { format } from 'small-date'

// Wednesday 03 March 2021, 11:45 am
format(new Date(), 'DDD dd MMMM yyyy, hh:mm a')

// Mittwoch 03 März 2021, 11:45
format(new Date(), 'DDD dd MMMM yyyy, HH:mm', {
  locale: 'de-DE',
})

Escaping Text

Sometimes we want to add arbitary text into our pattern. The problem is: Words break if they include tokens. Therefore, it is safest to escape all words with " quotes.

import { format } from 'small-date'

// Today is Wednesday the 03. of March
format(new Date(), '"Today is" DDD "the" dd. "of" MMMM')

TimeZone

We can pass a timeZone to the configuration to get the locale date at this particular zone.

import { format } from 'small-date'

// Wednesday 03 March 2021, 09:45 pm
format(new Date(), 'DDD dd MMMM yyyy, hh:mm a', {
  timeZone: 'Australia/Sydney',
})

Pattern Tokens

All examples below use the following date: new Date(2021, 2, 3, 18, 7, 8, 9) and the en-US locale.

Token DescriptionExample
DWeekday, 1 letterW
DDWeekday, 3 lettersWed
DDDWeekday, longWednesday
dDay of the month, no padding3
ddDay of the month, padded to 203
MMonth, numeric3
MMMonth, 2 digits03
MMMMonth, 3 lettersMar
MMMMMonth, longMarch
yYear, numeric2021 
yyYear, 2 digits21
yyyyYear, numeric2021
hHours, no padding6
hhHours, padded to 206
HHours in 24-format, no padding18
HHHours in 24-format, padded to 218
mMinutes, no padding7
mMinutes, padded to 207
sSeconds, no padding8
ssSeconds, padded to 208
SMilliseconds, no padding9
SSMilliseconds, padded to 209
SSSMilliseconds, padded to 3009
GEra, narrowA
GGEra, short AD
GGGEra, long Anno Domino
ZTime zone, shortGMT+1 
ZZTime short, longCentral European Standard Time
PPeriod of the day, narrowin the morning
PPPeriod of the day, shortin the morning
PPPPeriod of the day, longin the morning
aMeridiempm 

License

small-date is licensed under the MIT License. Documentation is licensed under Creative Common License. Created with ♥ by @robinweser and all the great contributors.