2.0.1 • Published 3 years ago

@eartharoid/dtf v2.0.1

Weekly downloads
227
License
MIT
Repository
github
Last release
3 years ago

dtf

GitHub Workflow Status

Simple and somewhat-localised timestamp formatting.

Note that in some cases, especially if you want to use standard formats and in other locales, it may be better to use the built-in Date or Intl functions.

Installation

Install with npm/pnpm/yarn:

npm i @eartharoid/dtf

Usage

const DTF = require('@eartharoid/dtf');
const dtf = new DTF();

// a new `Date` will be created
const timestamp = dtf.fill('DD/MM/YYYY HH:mm:ss');
console.log(timestamp); // => `13/07/2021 14:44:51`

// custom `Date` and use UTC time
const timestamp = dtf.fill('DD/MM/YYYY HH:mm:ss', new Date(), true);
console.log(timestamp); // => `13/07/2021 13:44:51`

See DTF#fill.

Placeholders

PlaceholderDescriptionExample
YYYYThe full year number2021
YYThe short year number21
MMMMThe full month nameJuly
MMMThe short month nameJul
MMThe zero-padded month number07
MThe non-padded month number7
DDDDThe full day nameTuesday
DDDThe short day nameTue
DDThe zero-padded date number13
D_The date followed by its ordinal suffix13th
DThe non-padded date number13
HHThe zero-padded 24h hour number14
hhThe zero-padded 12h hour number02
hThe non-padded 12h hour number2
mmThe zero-padded minute number18
mThe non-padded minute number18
ssThe zero-padded second number09
sThe non-padded second number9
SSSThe zero-padded millisecond number638
AMPMAM or PMPM

API

new DTF(locale?)

Create a new DTF instance.

  • locale? - the locale of this DTF instance (default is 'en')

DTF#locale

The locale of this DTF instance.

DTF#fill(format?, date?, utc?)

Fill a given or the default timestamp format using the current date or the Date provided.

  • format? - the placeholder string (default is 'HH:mm:ss')
  • date? - the Date object used to fill the placeholders (defaults to a new Date object)
  • utc? - use UTC time? (default is false)

DTF#AMPM(date?, utc?)

Returns 'AM' or 'PM' based on the time

  • date? - the date to use (defaults to a new Date object)
  • utc? - use UTC time? (default is false)

DTF#suffix(number)

Appends st, nd, rd, or th to the given number for an English locale. I don't know what it does for other locales.

  • number - the number to append the ordinal suffix to

DTF#convert24To12(h)

Converts a 24h number to 12h.

  • h - the 24h number

Support

Discord support server

Donate

ko-fi

or sponsor me.

License

MIT license.

© 2021 Isaac Saunders