ms: Tiny Millisecond Module by Guillermo Rauch

By  on  

This code snippet is now available with a GitHub repository: https://github.com/guille/ms.js

I wanted to pass along a simple but useful module that was posted as a Gist to GitHub by Guillermo Rauch.  This simple module, available via NPM as ms, provides a simple function for turning a human-readable string into milliseconds.

// Created by milliseconds

/**

# ms.js

No more painful `setTimeout(fn, 60 * 4 * 3 * 2 * 1 * Infinity * NaN * '☃')`.

    ms('2d')      // 172800000
    ms('1.5h')    // 5400000
    ms('1h')      // 3600000
    ms('1m')      // 60000
    ms('5s')      // 5000
    ms('500ms')   // 500
    ms('100')     // 100
    ms(100)       // 100

**/

(function (g) {
  var r = /(\d*.?\d+)([mshd]+)/
    , _ = {}

  _.ms = 1;
  _.s = 1000;
  _.m = _.s * 60;
  _.h = _.m * 60;
  _.d = _.h * 24;

  function ms (s) {
    if (s == Number(s)) return Number(s);
    r.exec(s.toLowerCase());
    return RegExp.$1 * _[RegExp.$2];
  }

  g.top ? g.ms = ms : module.exports = ms;
})(this);

A very nice little gem by Guiller.  Keep this function handy to save yourself from math expressions for time calculations!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    Build a Calendar Using PHP, XHTML, and CSS

    One of the website features my customers love to provider their web users is an online dynamic calendar. An online calendar can be used for events, upcoming product specials, memos, and anything else you can think of. I've taken some time to completely...

  • By
    dat.gui:  Exceptional JavaScript Interface Controller

    We all love trusted JavaScript frameworks like MooTools, jQuery, and Dojo, but there's a big push toward using focused micro-frameworks for smaller purposes. Of course, there are positives and negatives to using them.  Positives include smaller JS footprint (especially good for mobile) and less cruft, negatives...

Discussion

  1. Olmo

    Also note this has been accepted in MooTools More: https://github.com/mootools/mootools-more/pull/1098/files

  2. Ssss

    Whats with th ‘☃’ char?

    Have I missd something? xD

  3. Great one! Will definitely use this in my projects. Thanks a lot David!

  4. Hi David,

    First of all, nice site, I’ve been following you for the past 2 years or so.

    Yesterday Guille created a repo so that I could fork the code and make a pull request.
    Guille merged today my pull request because it’s a better rewrite overall:
    – Smaller footprint
    – 2.8x faster
    – Passes all unit tests

    The repo is here: https://github.com/guille/ms.js

    Could you update the article? It’d be great for everyone!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!