Crystal Reports 2008 : hacking datetime parameter format

One would think that this should be a no-brainer : ability to specify an edit format for date/time parameter in Crystal Reports. Not so fast…

Problem is as following:

ASP.Net Web client with Crystal Reports Viewer control is used.

When our report is based of some stored procedure which uses datetime parameter, it will inherit it as is and when report parameter entry form is displayed we would be asked to enter both date and time portion.

While, in general, it is OK, some reports are really interested only in date part being entered and time should be always set to 00:00:00. So we are forced to type time every time because if it is not provided calendar control would try to set time part initially to current time.

After some research off the Internet, visiting SAP forums, it is appear to be engine limitation. But we would want to overcome it somehow.

DISCLAIMER: solution below could only be used as a last resort and only if your business logic requires using date only params. And while it is only applies to calendar control, it is not per se a generic solution. This is a first “rough” attempt of achieving desired result and may lack some features otherwise expected.

It has been reported that solution above only works for current month. Since we are now using Crystal Reports 2008 with SP1, I would address this issue in my new post below.

IMPORTANT! With release of Service Pack 1 for Crystal Reports 2008, method blow is no longer applicable since structure of the files has changed. Please refer to this new post for more information.

Otherwise, Where there is a will, there is a way…

Fortunately we have access to internals behind the parameter handlers in Crystal Report Viewer web control.

Navigate to one of following locations:

  • C:\Inetpub\wwwroot\aspnet_client\system_web\2_0_50727\crystalreportviewers12\
  • C:\windows\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles\crystalreportviewers12\
  • C:\Program Files\Business Objects\Common\4.0\crystalreportviewers12\

and locate the file called allInOne.js – this is our victim. This file contains all JS code behind for CRV control which would be used by your web-site.

IMPORTANT: always create a backup copy before making any modifications.

Remember that our objective is to ensure that when calendar control is used time part of date/time value is always set as 00:00:00. Let’s add a new function to bobj.crv.Calendar object

/**
 * Reset time part of datetime to 00:00:00
 */
bobj.crv.Calendar._resetTime = function(date) {
    var newdate = this._copyDate(date);
    newdate.setHours(0);
    newdate.setMinutes(0);
    newdate.setSeconds(0);
    newdate.setMilliseconds(0);
    return newdate;
}

In my case I have put it just before bobj.crv.Calendar.justInTimeInit = function()  (line 39792).

As you can see the main purpose of the function is create a copy of date value with time portion being reset to desired time.

Now we would be looking for any instance in the code where the following substring is present

_timeField.setValue

We would find 3 functions where the following adjustment would be made:

bobj.crv.Calendar.justInTimeInit = function() {
...
    // mod: reset time
    this.date = this._resetTime(this.date);
    // mod
    this._timeField.setValue(bobj.external.date.formatDate(this.date, this._curTimeFormat));
...
}
...
bobj.crv.Calendar.justInTimeInit = function(){
...
    // mod: reset time
    this.date = this._resetTime(this.date);
    // mod
    this._timeField.setValue(bobj.external.date.formatDate(this.date, this._curTimeFormat));
...
}
...
bobj.crv.Calendar._onTimeChange = function() {
...
    if (date) {
        this._curTimeFormat = format;
        // mod: reset time
        date = this._resetTime(date);
        // mod
        this.date.setHours(date.getHours());
...
    else {
        // mod: reset time
        this.date = this._resetTime(this.date);
        // mod
...
}
...
bobj.crv.Calendar.setDate = function(date) {
    // mod: assign and reset time
    this.date = this._resetTime(date);
...
}

After all changes above are done, we would get a calendar control which “keeps time at bay”. This only affects calendar itself and not an edit control associated with the parameter. As a result we can still adjust time if choose to do so.

Last step would be to ensure that all allInOne.js file instances are in sync and we got desired functionality.

This entry was posted in Crystal Reports and tagged , , , , , . Bookmark the permalink.

Leave a Reply