Giter Site home page Giter Site logo

fast-date-format's Introduction

fast-date-format

Build Status Coverage Status NPM version Greenkeeper badge JavaScript Style Guide

fast-date-format is significantly faster than other date formatting modules.

Installation

npm install fast-date-format

Example

const DateFormat = require('fast-date-format');

const dateFormat = new DateFormat('YYYYMMDD');

dateFormat.format();
dateFormat.format(new Date());

Escaping

To escape characters in a format, surround the characters with square brackets.

const dateFormat = new DateFormat('[Month: ]MMMM');

dateFormat.format(); // Month: December

Tokens

Supported formatting tokens:

Token Example Description
YYYY 2018 Year
YY 18 2 Digit Year
MMMM January..December Month name
MMM Jan..Dec 3 letter month name
MM 01..12 Month
M 1..12 Month
HH 00..23 Hours (24 hour time)
H 0..23 Hours (24 hour time)
hh 01..12 Hours (12 hour time)
h 1..12 Hours (12 hour time)
kk 01..24 Hours (24 hour time)
k 1..24 Hours (24 hour time)
A am, pm Post or ante meridiem
dddd Monday..Sunday Day name
ddd Mon..Sun 3 letter day name
DDDD 001..365 Day of year
DDD 1..365 Day of year
DD 01..31 Day of month
D 1..31 Day of month
E 1..7 Day of week
m 0..59 Minutes
mm 00..59 Minutes
s 0..59 Seconds
ss 00..59 Seconds
SSS 000..999 Milliseconds
ZZ -02:00 Offset from UTC
Z -0200 Offset from UTC
x 1528643900952 Unix timestamp in ms

API

DateFormat(options)

Creates a new date formatter.

options

Either a date format as a string or an object with the following properties:

dateFormat

A date format as a string.

cache (optional)

Enables caching to increase formatting speed if set to true. Defaults to false.

DateFormat#format([date])

Formats the date according to the dateFormat. If no date is passed the current date is used.

DateFormat#addLocale(language, data)

Adds translations for the names of months and weekdays.

dateFormat.addLocale('en', {
  weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
})

Three letter abbreviations of the names are being generated from the months and weekdays.

DateFormat#setLocale(language)

Changes the locale of the DateFormat instace to language. Build-in languages: en & de.

dateFormat.addLocale('en')

Benchmark

$ node benchmark/benchmark.js
moment x 226,501 ops/sec ±0.74% (87 runs sampled)
date-format x 276,927 ops/sec ±0.61% (86 runs sampled)
dateformat x 112,760 ops/sec ±1.60% (85 runs sampled)
fast-date-format x 2,207,410 ops/sec ±0.62% (90 runs sampled)
fecha x 280,415 ops/sec ±1.27% (89 runs sampled)
speed-date x 2,035,050 ops/sec ±1.25% (89 runs sampled)
Fastest is fast-date-format

With activated cache option:

$ node benchmark/benchmarkCache.js
moment x 288,008 ops/sec ±1.88% (82 runs sampled)
date-format x 288,106 ops/sec ±0.52% (85 runs sampled)
dateformat x 131,345 ops/sec ±1.36% (83 runs sampled)
fast-date-format x 4,014,322 ops/sec ±1.94% (85 runs sampled)
fecha x 355,271 ops/sec ±1.46% (86 runs sampled)
speed-date x 2,670,824 ops/sec ±0.74% (90 runs sampled)
Fastest is fast-date-format

License

MIT

fast-date-format's People

Contributors

cheragv avatar greenkeeper[bot] avatar serayaeryn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.