Skip to content

Instantly share code, notes, and snippets.

@srounce
Created September 17, 2012 15:46
Show Gist options
  • Select an option

  • Save srounce/3738115 to your computer and use it in GitHub Desktop.

Select an option

Save srounce/3738115 to your computer and use it in GitHub Desktop.
Calculate time until/since etc...
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require) {
"use strict";
var CountDown,
WEEK_MILLIS, DAY_MILLIS, HOUR_MILLIS, MINUTE_MILLIS;
MINUTE_MILLIS = 1000 * 60;
HOUR_MILLIS = MINUTE_MILLIS * 60;
DAY_MILLIS = HOUR_MILLIS * 24;
WEEK_MILLIS = DAY_MILLIS * 7;
//
CountDown = function()
{
};
CountDown.timeLeft = function( from, until )
{
var diff,
timeLeft = {};
if( !(from instanceof Date) ) from = new Date(from);
if( !(until instanceof Date) ) until = new Date(until);
diff = until - from;
timeLeft.weeks = Math.floor( diff/WEEK_MILLIS );
diff -= timeLeft.weeks * WEEK_MILLIS;
timeLeft.days = Math.floor( diff/DAY_MILLIS );
diff -= timeLeft.days * DAY_MILLIS;
timeLeft.hours = Math.floor( diff/HOUR_MILLIS );
diff -= timeLeft.hours * HOUR_MILLIS;
timeLeft.minutes = Math.floor( diff/MINUTE_MILLIS );
diff -= timeLeft.minutes * MINUTE_MILLIS;
timeLeft.seconds = Math.floor( diff/1000 );
diff -= timeLeft.seconds * 1000;
timeLeft.millis = Math.floor( diff );
return timeLeft;
};
return CountDown;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment