Skip to content

Instantly share code, notes, and snippets.

@Kruithne
Last active January 22, 2016 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kruithne/a1dd2b3946e3d65472fd to your computer and use it in GitHub Desktop.
Save Kruithne/a1dd2b3946e3d65472fd to your computer and use it in GitHub Desktop.
Time Machine!
var timeMachine =
{
minute: 60,
hour: 3600,
day: 86400,
week: 604800,
month: 2419200,
year: 29030400,
negMinute: -60,
negHour: -3600,
negDay: -86400,
negWeek: -604800,
negMonth: -2419200,
negYear: -29030400,
monthNames: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
divide: function(difference, period)
{
return Math.floor(difference, period);
},
suffix: function(day)
{
var j = day % 10;
if (j == 1 && day != 11)
return day + "st";
if (j == 2 && day != 12)
return day + "nd";
if (j == 3 && day != 13)
return day + "rd";
return day + "th";
},
timestampToDate: function(timestamp)
{
return new Date(timestamp * 1000);
},
parseDate: function(date)
{
if (date instanceof Date)
return date;
if (typeof(date) == "string")
date = parseInt(date);
return timeMachine.timestampToDate(date);
},
getFormalDateString: function(date)
{
var t = timeMachine;
date = t.parseDate(date);
return t.dayNames[date.getDay()] + " " + t.suffix(date.getDate()) + " " + t.monthNames[date.getMonth()] + " " + date.getFullYear();
},
getFormalTimePeriod: function(date)
{
var t = timeMachine;
date = t.parseDate(date);
var difference = (date.getTime() - new Date().getTime()) / 1000, value = 0, text;
switch (true)
{
case difference > t.year:
value = t.divide(difference, t.year);
text = 'year';
break;
case difference > t.month:
value = t.divide(difference, t.month);
text = 'month';
break;
case difference > t.week:
value = t.divide(difference, t.week);
text = 'week';
break;
case difference > t.day:
value = t.divide(difference, t.day);
text = 'day';
break;
case difference > t.hour:
value = t.divide(difference, t.hour);
text = 'hour';
break;
case difference > t.minute:
value = t.divide(difference, t.minute);
text = 'minute';
break;
case difference < t.minute && difference > 0:
return 'less than a minute';
break;
case difference > t.negMinute && difference < 0:
return 'less than a minute ago';
break;
case difference > t.negHour:
value = t.divide(difference, t.negMinute);
text = 'minute';
break;
case difference > t.negDay:
value = t.divide(difference, t.negHour);
text = 'hour';
break;
case difference > t.negWeek:
value = t.divide(difference, t.negDay);
text = 'day';
break;
case difference > t.negMonth:
value = t.divide(difference, t.negWeek);
text = 'week';
break;
case difference > t.negYear:
value = t.divide(difference, t.negMonth);
text = 'month';
break;
case difference < t.negYear:
value = t.divide(difference, t.negYear);
text = 'year';
break;
}
if (value > 1)
text += 's';
return value + ' ' + text + (difference < 0 ? ' ago' : '');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment