Skip to content

Instantly share code, notes, and snippets.

@nonrational
Created October 24, 2011 22:35
Show Gist options
  • Save nonrational/1310578 to your computer and use it in GitHub Desktop.
Save nonrational/1310578 to your computer and use it in GitHub Desktop.
Days, Hours, Minutes, Seconds until Midnight at YYYY-MM-DD
function timeRemaining(yr, m, d) {
function pad(i) {
return (i < 10) ? "0" + i : "" + i;
}
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var theyear = yr, themonth = m, theday = d, today = new Date();
var todayy = today.getYear();
if (todayy < 1000){
todayy += 1900
}
var todaym = today.getMonth(), todayd = today.getDate(), todayh = today.getHours(), todaymin = today.getMinutes(), todaysec = today.getSeconds();
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
var futurestring = montharray[m - 1] + " " + d + ", " + yr;
dd = Date.parse(futurestring) - Date.parse(todaystring);
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
return pad(dday) + ":" + pad(dhour) + ":" + pad(dmin) + ":" + pad(dsec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment