Skip to content

Instantly share code, notes, and snippets.

@arian
Created February 14, 2012 20:37
Show Gist options
  • Save arian/1830162 to your computer and use it in GitHub Desktop.
Save arian/1830162 to your computer and use it in GitHub Desktop.
var re = /([\d.]+)(s|ms)?/, units = {ms: 1, s: 1e3};
var parseDuration = function(value){
var match = value.toString().match(re);
return match ? parseFloat(match[1]) * (units[match[2]] || 1) : parseFloat(value)
};
console.log(parseDuration(200));
console.log(parseDuration('200'));
console.log(parseDuration('200ms'));
console.log(parseDuration('.2s'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment