Skip to content

Instantly share code, notes, and snippets.

@tleen
Created August 20, 2013 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tleen/6276174 to your computer and use it in GitHub Desktop.
Save tleen/6276174 to your computer and use it in GitHub Desktop.
Using the moment.js time duration shorthand to get number of seconds from a string. Say the inputString is '3y' or '5M' or '2d', this would give you the respective time from now in seconds.
// I don't love this but its a quick/dirty create of cache duration using moment's nice date add format
// parse cache duration as [integer][unit] via http://momentjs.com/docs/#/manipulating/add/ shorthand
var durations = /^([\d]+)([\w]+)/.exec(inputString);
var invalidateSeconds = moment(0).add(durations[2],durations[1]).unix();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment