Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Created July 11, 2012 14:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save addyosmani/3090672 to your computer and use it in GitHub Desktop.
Save addyosmani/3090672 to your computer and use it in GitHub Desktop.
Time until
var timeUntil = {
inHours: function(d1, d2) {
return parseInt((d2.getTime()-d1.getTime())/(24*3600), 10);
},
inDays: function(d1, d2) {
return parseInt((d2.getTime()-d1.getTime())/(24*3600*1000), 10);
},
inWeeks: function(d1, d2) {
return parseInt((d2.getTime()-d1.getTime())/(24*3600*1000*7), 10);
},
inMonths: function(d1, d2) {
return (d2.getMonth()+12*d2.getFullYear())-(d1.getMonth()+12*d1.getFullYear());
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
var endDate = new Date("July, 20, 2012");
var startDate = new Date();
console.log(timeUntil.inDays(startDate,endDate));
console.log(timeUntil.inHours(startDate,endDate));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment