Return to Snippet

Revision: 65326
at November 20, 2013 10:16 by camdagr8


Updated Code
Date.prototype.lapsed = function (now) {
	now = now || new Date();

	var milliseconds = Math.abs(this.getTime() - now.getTime());

	var oneHour = 3600000;
	var oneMinute = 60000;
	var oneSecond = 1000;
	var hours = 0;
	var minutes = 0;
	var result;
	var seconds = 0;

	if (milliseconds >= oneHour) { hours = Math.floor(milliseconds / oneHour); }

	milliseconds = hours > 0 ? (milliseconds - hours * oneHour) : milliseconds;

	if (milliseconds >= oneMinute) { minutes = Math.floor(milliseconds / oneMinute); }

	milliseconds = minutes > 0 ? (milliseconds - minutes * oneMinute) : milliseconds;

	if (milliseconds >= oneSecond) { seconds = Math.floor(milliseconds / oneSecond); }

	milliseconds = seconds > 0 ? (milliseconds - seconds * oneSecond) : milliseconds;

	if (hours > 0) { result = (hours > 9 ? hours : "0" + hours) + ":"; }
	else { result = "00:"; }

	if (minutes > 0) { result += (minutes > 9 ? minutes : "0" + minutes) + ":"; }
	else { result += "00:"; }

	if (seconds > 0) { result += (seconds > 9 ? seconds : "0" + seconds); }
	else { result += "00"; }

	return result;
};

Revision: 65325
at November 20, 2013 10:14 by camdagr8


Initial Code
Date.prototype.lapsed = function (now) {
	now = now || new Date();

	var milliseconds = Math.abs(this - now.getTime());

	var oneHour = 3600000;
	var oneMinute = 60000;
	var oneSecond = 1000;
	var hours = 0;
	var minutes = 0;
	var result;
	var seconds = 0;

	if (milliseconds >= oneHour) { hours = Math.floor(milliseconds / oneHour); }

	milliseconds = hours > 0 ? (milliseconds - hours * oneHour) : milliseconds;

	if (milliseconds >= oneMinute) { minutes = Math.floor(milliseconds / oneMinute); }

	milliseconds = minutes > 0 ? (milliseconds - minutes * oneMinute) : milliseconds;

	if (milliseconds >= oneSecond) { seconds = Math.floor(milliseconds / oneSecond); }

	milliseconds = seconds > 0 ? (milliseconds - seconds * oneSecond) : milliseconds;

	if (hours > 0) { result = (hours > 9 ? hours : "0" + hours) + ":"; }
	else { result = "00:"; }

	if (minutes > 0) { result += (minutes > 9 ? minutes : "0" + minutes) + ":"; }
	else { result += "00:"; }

	if (seconds > 0) { result += (seconds > 9 ? seconds : "0" + seconds); }
	else { result += "00"; }

	return result;
};

Initial URL

                                

Initial Description
Calculates the time in hh:mm:ss between two dates

Usage: 
var past = new Date('1/1/2013'); 
var now = new Date();

var lapsed = past.lapsed(now);

Initial Title
Date.lapsed(Date)

Initial Tags
javascript, date

Initial Language
JavaScript