Skip to content

Instantly share code, notes, and snippets.

@LucasLemanowicz
Created August 8, 2014 01:08
Show Gist options
  • Save LucasLemanowicz/14e10426da4ae14c3a09 to your computer and use it in GitHub Desktop.
Save LucasLemanowicz/14e10426da4ae14c3a09 to your computer and use it in GitHub Desktop.
UnPrettyDate
/*
* JavaScript Un-PrettyDate
* Copyright (c) 2014 Lucas Lemanowicz
* Licensed under the MIT license.
*/
// Takes a PrettyDate string and turns it back into a Date object.
function unPrettyDate(time) {
timeCut = time.split(" ");
// Assumes input is in the format of "X units ago"
if (timeCut.length == 3 && timeCut[2] == "ago") {
switch(timeCut[1].toLowerCase()) {
default:
case "second":
case "seconds":
multiplier = 1;
break;
case "minute":
case "minutes":
multiplier = 60;
break;
case "hour":
case "hours":
multiplier = 60 * 60;
break;
case "day":
case "days":
multiplier = 60 * 60 * 24;
break;
case "week":
case "weeks":
multiplier = 60 * 60 * 24 * 7;
break;
case "month":
case "months":
multiplier = 60 * 60 * 24 * 7 * 30.5;
break;
}
// Determine roughly how many seconds ago this was
diffSeconds = parseInt(timeCut[0]) * multiplier;
return new Date(new Date() - diffSeconds * 1000);
}
throw "Unsupported Pretty Date format";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment