Created
December 14, 2010 21:28
-
-
Save gerardpaapu/741134 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is my new favourite version