Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- };
Add Comment
Please, Sign In to add comment