Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. var parsedate = function (str) {
  2. // parsedate: String -> Date or null
  3. //
  4. // Decodes the timestamps from the ihackernews api, it seems like
  5. // they're produced by the following proc from news.arc.
  6. //
  7. // (def text-age (a)
  8. // (tostring
  9. // (if (>= a 1440) (pr (plural (trunc (/ a 1440)) "day") " ago")
  10. // (>= a 60) (pr (plural (trunc (/ a 60)) "hour") " ago")
  11. // (pr (plural (trunc a) "minute") " ago"))))
  12. var now, match, num, scale, diff, units;
  13.  
  14. match = /([0-9]+) ((day)|(hour)|(minute))s? ago/.exec(str);
  15.  
  16. if (match === null) {
  17. return null;
  18. }
  19.  
  20. units = {
  21. minute: 60000,
  22. hour: 3600000,
  23. day: 86400000
  24. };
  25.  
  26. num = Number(match[1]);
  27. scale = units[ match[2] ];
  28. diff = num * scale;
  29. now = Date.now();
  30.  
  31. return new Date(now - diff);
  32. };
Add Comment
Please, Sign In to add comment