How to format date field if using Summary report template designer

This post applies to date fields in a summary type report template, also custom date fields, and in some cases if when using just the Format property in a detailed type report template for a date field does not correct format it.

Right now if using a summary report template, date fields can not be formatted to display other than they do within the AyaNova program. Also occurs with some date fields even if a detailed type report template. Also
In either case, if using the Format property of the DataBinding does not format your date datafield on your report, then use similar to the script shown below:

This sample is to show how to use a script and the date formatter to format a date field in a report template.

The sample report template Service Workorders Invoiced available from the Service Workorders grid in the Service navigation pane by default displays the Service Date in the format it does within your AyaNova program as per the screen shot below.

Let’s say we want it to instead display in the format dddd, dd MMMM yyyy HH:mm:ss such as Thursday 16 November 2006 11:00:14

If a detailed report template, all we would do is set the FormatString property for the datafield via the Properties tab as in the screenshot below, but because this is a summary report template we also need to include a script that converts the date field from a string to a recognizable date so it can be formatted.

As this is a summary report template also applies to custom date fields, we include the script in the OnBeforePrint script and set the date fields FormatString property

  1. Open the existing sample Service Workorders Invoice summary report template and perform the same steps as below. Any issues, download this completed sample report template via the link below so that you may see what has been done to it and the script used.

Sample Service Workorders Invoiced with Date Format

For use with AyaNova 6 or 7 Sample Service Workorders Invoiced with Date Format

  1. Click on the Service Date field in the designer panel, and select the Properties tab

  2. Expand the Scripts property and select to open the script editor for the OnBeforePrint for this field

  1. Copy all text below and paste into the OnBeforeScript editor between the { } for the OnBeforePrint for that field and select OK to save

//This code takes a summary report date field which is not a DateTime object
//converts it to a DateTime object, then sets the label control’s text to
//the format selected in the databinding.FormatString property of the control

//Get a reference to the label on the report
XRLabel lbl = sender as XRLabel;

//If it’s empty then no need to format it
if(lbl.Text=="") return;

//It’s not empty, so parse it’s text into a DateTime object

//Create a datetime object to hold the date
DateTime dt = new DateTime();

//First try to parse it using the current thread’s locale setting:
//if that fails to parse then try the invariant culture format
if (!DateTime.TryParse(lbl.Text, out dt))
if (!DateTime.TryParse(lbl.Text, System.Globalization.CultureInfo.InvariantCulture. DateTimeFormat, System.Globalization.DateTimeStyles.None, out dt))
{
//if the code makes it to here then the date/time was not recognized
//and the code can not proceed. An alternative to this is to simply return
//without changing the lbl.Text by commenting out the following line
//this will ensure that something is printed even if not formatted
lbl.Text=“Unrecognized date”;
return;
}

//At this point we have a recognized date object and can format it as desired
//using a date format string in the ToString method of the date time object.
//To see other examples of date format string codes check this out:
//http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
lbl.Text=dt.ToString(“dddd, dd MMMM yyyy HH:mm:ss”);

  1. Note the last line of the code - that is where you dictate how you want your date string to show. So for example, if you want only the date and no time, then the last line would be lbl.Text=dt.ToString(“dddd, dd MMMM yyyy”);

[i]NOTE: in the downloadable example report template in this forum post, the last line is:

lbl.Text=string.Format(lbl.DataBindings[0].FormatString,dt);

which is to tell the report to use what has been set in the Format String property.
You can instead JUST use the script as above without having to set the Format String property[/i]

  1. Now preview the report template to ensure your formatting has been applied.