0

Date Math in Microsoft Dynamics CRM

by BenVollmer 28. May 2010 21:08

image In case you haven’t noticed, handling dates in JScript is less than ideal. In the book I keep here at my side, there is easily two chapters on handling dates in JScript. (Mind you, not CRM, just regular old JScript.) And then throw doing some math on dates, and it hates you. And you can burn some time in trying to troubleshoot everything.

For a demo, we wanted to take a start date, add a number of days to it and then automatically create the end date. And several hours later, I called my “LifelineChet Kloss from Michigan. In about 10 minutes Chet had my code fixed up.

It turns out in the last Variable, I failed to tell JScript this was a date, which threw it in a bit of a tizzy. So Chet fixed that for me and low and behold, it actually works.

The trick for me at least was converting the number of days into Milliseconds, which is the least common denominator in JScript.

So in the OnChange of Number of Days this was put in the OnChange

var Days = parseInt(crmForm.all.new_numberofdays.DataValue);
var Start = crmForm.all.new_startdate.DataValue;
var Hours = Days * 24;
var Minutes = Hours * 60;
var Seconds = Minutes * 60;
var Milliseconds = Seconds * 1000;
var Finish =  new Date(Start.getTime() + Milliseconds);
crmForm.all.new_enddate.DataValue =Finish;

In the OnChange of Start Date the following Code was placed

crmForm.all.new_numberofdays.FireOnChange();

And in the OnSave Event for the Form, the following code was used:

crmForm.all.new_enddate.ForceSubmit = true;

Tags: ,

Demo Tools

Powered by BlogEngine.NET 1.6.1.0
Original Design by Laptop Geek, Adapted by onesoft