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;
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: