GithubHelp home page GithubHelp logo

lukeed / tinydate Goto Github PK

View Code? Open in Web Editor NEW
1.1K 16.0 24.0 49 KB

A tiny (349B) reusable date formatter. Extremely fast!

Home Page: https://jsfiddle.net/lukeed/aoy0xeze/

License: MIT License

JavaScript 100.00%
javascript date datetime format time timestamp

tinydate's Introduction

tinydate CI licenses downloads

A tiny (349B) reusable date formatter. Extremely fast!

Demo

Inspired by tinytime, this module returns a "render" function that efficiently re-render your deconstructed template. This allows for incredibly performant results!

However, please notice that this only provides a limited subset of Date methods.
If you need more, tinytime or date-fns are great alternatives!

Install

$ npm install --save tinydate

Usage

const tinydate = require('tinydate');
const fooDate = new Date('5/1/2017, 4:30:09 PM');

const stamp = tinydate('Current time: [{HH}:{mm}:{ss}]');

stamp(fooDate);
//=> Current time: [16:30:09]

stamp();
//=> Current time: [17:09:34]

API

tinydate(pattern, dict?)(date?)

Returns: Function

Returns a rendering function that will optionally accept a date value as its only argument.

pattern

Type: String
Required: true

The template pattern to be parsed.

dict

Type: Object
Required: false

A custom dictionary of template patterns. You may override existing patterns or declare new ones.

Important: All dictionary items must be a function and must control its own formatting.
For example, when defining your own {ss} template, tinydate will not pad its value to two digits.

const today = new Date('2019-07-04, 5:30:00 PM');

// Example custom dictionary:
//   - Adds {MMMM}
//   - Overrides {DD}
const stamp = tinydate('Today is: {MMMM} {DD}, {YYYY}', {
	MMMM: d => d.toLocaleString('default', { month: 'long' }),
	DD: d => d.getDate()
});

stamp(today);
//=> 'Today is: July 4, 2019'

date

Type: Date
Default: new Date()

The date from which to retrieve values. Defaults to current datetime if no value is provided.

Patterns

  • {YYYY}: full year; eg: 2017
  • {YY}: short year; eg: 17
  • {MM}: month; eg: 04
  • {DD}: day; eg: 01
  • {HH}: hours; eg: 06 (24h)
  • {mm}: minutes; eg: 59
  • {ss}: seconds; eg: 09
  • {fff}: milliseconds; eg: 555

Benchmarks

# Node v10.13.0

tinydate    x 160,834,214 ops/sec ±0.21% (96 runs sampled)
tinytime    x  44,602,162 ops/sec ±0.34% (97 runs sampled)
time-stamp  x     888,153 ops/sec ±1.27% (86 runs sampled)

License

MIT © Luke Edwards

tinydate's People

Contributors

danielbayerlein avatar lukeed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tinydate's Issues

Suggestion: Add support for passing custom `dict`

Great work with on that awesome library!
I have a suggestion, add support for customizing the dict used when compiling a formatter.
That main purpose being supporting UTC dates (ie. getUTCFullYear, ...)

TypeScript Definitions

With the import statement I get a warning in vscode saying

import tinydate from 'tinydate';

module "/Users/###/###/node_modules/tinydate/dist/tinydate"
Could not find a declaration file for module 'tinydate'. '/Users/###/###/node_modules/tinydate/dist/tinydate.js' implicitly has an 'any' type.
Try npm install @types/tinydate if it exists or add a new declaration (.d.ts) file containing declare module 'tinydate';ts(7016)

image

Add export map

Hi @lukeed! It would be great to have an export map for this project to be able to import it as ESM without depending on the module property!

Let me know if you want a PR! The one thing I'm not sure about is how we'd want to expose the minified file in dist? Or if that file is even needed?

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.