Pogo69's Blog

May 5, 2011

The Beginnings of a Date Library

Filed under: Cutting Code, Javascript — pogo69 [Pat Janes] @ 09:34

Recently in one of the CRM Forums, there was a request for some javascript to find the differences between two dates.  I’d started to work up a JScript Date Library that does exactly that, so I figured I’d share it here for all:

if (typeof DateLib == 'undefined') {
	DateLib = {};
}

DateLib.Diff = function (from, to) {
	return to - from;
}
DateLib.DiffMinutes = function (from, to) {
	return DateLib.Diff(from, to) / (1000 * 60);
}
DateLib.DiffParts = function (from, to) {
	var diff = DateLib.DiffMinutes(from, to);

	var parts = new Object();

	parts.Days = Math.floor(diff / (60 * 24));
	diff -= (parts.Days * 60 * 24);
	parts.Hours = Math.floor(diff / 60);
	diff -= (parts.Hours * 60);
	parts.Minutes = Math.floor(diff);

	parts.Display =
			(parts.Days > 0 ? parts.Days + " days, " : "") +
			(parts.Hours > 0 ? parts.Hours + " hrs, " : "") +
			(parts.Minutes + " mins");

	return parts;
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.