🚀 Socket Launch Week 🚀 Day 3: Socket Acquires Coana.Learn More
Socket
Sign inDemoInstall
Socket

speed-date

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

speed-date

Format dates super-efficiently

1.0.0
latest
Source
npm
Version published
Weekly downloads
388
-55.3%
Maintainers
1
Weekly downloads
 
Created
Source

speed-date

Travis Dependencies Join the chat at https://gitter.im/gosquared/speed-date

NPM

Format your dates really really fast

So you have a date object in JavaScript. And you want to turn it into a string. Perhaps you want to format it a particular way, say YYYY-MM-DD HH:mm:ss.

Speed-date is a super-fast date formatter optimized for formatting lots of dates with the same format.

Installation

npm install speed-date

Usage

var speedDate = require('speed-date');

var theDate = new Date(2006, 1, 3, 12, 34, 56);

var formatter = speedDate('YYYY-MM-DD HH:mm:ss');

formatter(theDate); // '2006-02-03 12:34:56'


// alternatively:
speedDate('YYYY-MM-DD HH:mm:ss', theDate); // '2006-02-03 12:34:56'

// alternatively alternatively:
// faster when repeated lots with the same format,
// useful as an alternative to keeping track of formatter as above
speedDate.cached('YYYY-MM-DD HH:mm:ss', theDate); // '2006-02-03 12:34:56'

Full API

speedDate(format[, date])

When called with just format, generates a formatter function that takes date objects.

When called with both format and date, creates a formatter function and applies it with the given date.

speedDate.cached(format[, date])

Behaves the same as speedDate, but the generated formatter function is stored and reused if the same format is used again. Only use this if you know you're only going to be using a few different possible formats (they're never cleaned up internally).

speedDate.UTC(format[, date])

Exactly the same as above, but always interprets the date as UTC. Obviousy, in this mode, Z and ZZ tokens will always be +00:00 and +0000 respectively. Similarly speedDate.UTC.cached(format[, date]).

But how fast is it actually?

Well, run npm run benchmark to find out for yourself how it stacks up against Moment for various formatting tokens. On the whole speed-date is ~15-20x faster than Moment with repeated use of the same formatter function. Check out benchmark results for an example run.

Is there any error handling or input validation?

Nope. None at all. In the interests of performance, speed-date assumes that you're always passing in valid Date objects and sane formatting strings. If you give it an invalid Date, it won't handle it nicely, like how Moment returns "Invalid date". It'll probably return something with NaN and undefined all over the place. If you give it something that isn't a Date object at all, there's no telling what it'll do. It might return complete garbage, it might throw an error, it might set fire to your dog. So be careful.

FAQs

Package last updated on 11 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts