Skip to content

Instantly share code, notes, and snippets.

@gerardpaapu
Created December 14, 2010 21:28
Show Gist options
  • Save gerardpaapu/741134 to your computer and use it in GitHub Desktop.
Save gerardpaapu/741134 to your computer and use it in GitHub Desktop.
var parsedate = function (str) {
// parsedate: String -> Date or null
//
// Decodes the timestamps from the ihackernews api, it seems like
// they're produced by the following proc from news.arc.
//
// (def text-age (a)
// (tostring
// (if (>= a 1440) (pr (plural (trunc (/ a 1440)) "day") " ago")
// (>= a 60) (pr (plural (trunc (/ a 60)) "hour") " ago")
// (pr (plural (trunc a) "minute") " ago"))))
var now, match, num, scale, diff, units;
match = /([0-9]+) ((day)|(hour)|(minute))s? ago/.exec(str);
if (match === null) {
return null;
}
units = {
minute: 60000,
hour: 3600000,
day: 86400000
};
num = Number(match[1]);
scale = units[ match[2] ];
diff = num * scale;
now = Date.now();
return new Date(now - diff);
};
@gerardpaapu
Copy link
Author

I think this is my new favourite version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment