项目作者: VitorLuizC

项目描述 :
:calendar: A small library (around 400 B when gziped & minified) to format JavaScript `Date` object using same tokens as moment.
高级语言: TypeScript
项目地址: git://github.com/VitorLuizC/format-date.git
创建时间: 2018-06-16T19:08:48Z
项目社区:https://github.com/VitorLuizC/format-date

开源协议:MIT License

下载


@bitty/format-date

@bitty/format-date"">Bundle minified size
@bitty/format-date"">Bundle minified and gzipped size

@bitty/format-date is a small library (around 400 B when gziped & minified) to format JavaScript Date object using same tokens as moment.

  • 📦 Distributions in ESM, CommonJS, UMD and UMD minified formats.

  • ⚡ Lightweight:

    • Weighs around 0.4KB (min + gzip).
  • 🔋 Bateries included:

    • No dependencies.
    • Its not based on newer browser’s APIs or es2015+ features.
  • 🏷 Safe:

    • JSDocs and type declarations for IDEs and editor’s autocomplete/intellisense.
    • Made with TypeScript as strict as possible.
    • Unit tests with AVA.

Installation

This library is published in the NPM registry and can be installed using any compatible package manager.

  1. npm install @bitty/format-date --save
  2. # For Yarn, use the command below.
  3. yarn add @bitty/format-date

Installation from CDN

This module has a UMD bundle available through JSDelivr and Unpkg CDNs.

  1. <!-- For UNPKG use the code below. -->
  2. <script src="https://unpkg.com/@bitty/format-date"></script>
  3. <!-- For JSDelivr use the code below. -->
  4. <script src="https://cdn.jsdelivr.net/npm/@bitty/format-date"></script>
  5. <script>
  6. // UMD module is exposed through the "isNullish" global function.
  7. console.log(formatDate);
  8. //=> "[Function: formatDate]"
  9. console.log(formatDate(new Date(), 'YYYY-MM-DD'));
  10. //=> "2020-06-14"
  11. </script>

Usage

@bitty/format-date exports a function to format JavaScript Date object using moment tokens.

  1. import formatDate from '@bitty/format-date';
  2. formatDate(new Date(), 'DD/MM/YYYY HH [hours] [and] mm [minutes].');
  3. // I'm escaping "hours", "and" and "minutes" using same syntax as Moment.js.

Tokens

Right now this lib supports the tokens below.

Token Output
Year YY 70, 71, … 19, 20.
YYYY 1970, 1971, … 2019, 2020.
Month M 1, 2, … 11, 12.
MM 01, 02, … 11, 12.
Day of Month D 1, 2, … 30, 31.
DD 01, 02, … 30, 31.
Hour H 1, 2, … , 22,23.
HH 01, 02, … , 22,23.
Minute m 1, 2, … , 58,59.
mm 01, 02, … , 58,59.
Second s 1, 2, … , 58,59.
ss 01, 02, … , 58,59.
AM/PM A AM, PM.
a am, pm.
Escape [*]

License

Released under MIT License.