CRM 2011 – Javascript Date Difference Code

The excellent Pogo69 blog, which is an excellent resource for CRM developers has a blog post with a nice piece of Javascript to find the difference between two dates using Javascript.

He mentions that he is starting to build a Javascript library, which thanks to CRM 2011 we can now do because you can use the same Javascript file in many forms.

I am surprised that Microsoft haven’t already created some general Javascript libraries for everyone to use, surely their developers would have already done something like that or at least created enough Javascript to make one.

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;

8 thoughts on “CRM 2011 – Javascript Date Difference Code

  1. jquery October 14, 2011 / 6:21 am

    I simply couldn’t go away your site before suggesting that I actually enjoyed the usual info an individual supply on your guests? Is going to be again frequently to investigate cross-check new posts


    • Hosk October 14, 2011 / 3:09 pm

      I’m not entirely sure what you mean


      • Adam July 21, 2015 / 7:20 pm

        Hosk could you do a quick write up to walk through this code please so I can better understand it? I’m new, thanks so much your posts help me learn a lot, I can’t stress enough how much I appreciate it.


  2. PJ March 11, 2013 / 4:40 am

    I think he said “Couldn’t leave without saying how much I enjoyed the info that you supply to your guests, I’ll be calling back to check out new posts as they arrive…”


  3. Arul August 17, 2016 / 6:53 am

    Hi friend

    I need to create a new notes control without attachment option

    Please help me .how to create


    • Hosk August 17, 2016 / 8:03 am

      You can edit the existing notes functionality so you will need to create new functionality. I would probably create a new entity


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.