Did you know that different countries have different date formats? When you make a multilingual report, or a report for a country where they speak Persian (Farsi), it is important to bring up dates in the correct format. By default, FastReport uses the European date format, but the .NET tools allow converting it into different formats.
Thus, our task is to convert the date into the Persian format. For example, our report has an expression [Date], bringing up today’s date:
The expression [Date] gets the current system date in the DateTime format, but the value of the text report after all processing is the String. Let us create a new function in the report script:
private void ConvertToPersianDate(object sender, EventArgs e)
We will evoke this function from text objects. We create a temporary variable, which will convert the text of the object into DateTime:
DateTime d = DateTime.Parse((sender as TextObject).Text);
“sender as TextObject” is the address to the object which evoked the function. We may use the functions and properties of an object if we address it in such a way.
After that, we will need the PersianCalendar object, which will convert the date into the Persian format:
PersianCalendar pc = new PersianCalendar();
Note that this object is stored in the System.Globalization library, and it must be indicated in the “using” section.
Then we must change the text of the object. Consider this line in more detail:
(sender as TextObject).Text = string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d));
Here we set the text of our object. The value will be in the format year/month/day, because we use the functions of the PersianCalendar which get the respective values. The code section where the text is set can be edited as you wish. For example, the code for the date in the day.month.year format looks like this:
"{0}.{1}.{2}", pc.GetDayOfMonth(d), pc.GetMonth(d), pc.GetYear(d)
As a result, we get the following function:
using System.Globalization; namespace FastReport { public class ReportScript { private void ConvertToPersianDate(object sender, EventArgs e) { // Converting to DateTime format DateTime d = DateTime.Parse((sender as TextObject).Text); // Creating an object for conversion PersianCalendar pc = new PersianCalendar(); // Creating a string using PersianCalendar // It is possible to change the string template (sender as TextObject).Text = string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)); } } }
Add the function to the AfterData event of the relevant object.
Now the date looks like this:
To use other functions related to time, you can consult the following table:
GetDayOfWeek(), | Day of week |
GetMonth(), | Month |
GetDayOfMonth(), | Day of month |
GetYear(), | Year |
GetHour() | Hour |
GetMinute() | Minute |
GetSecond() | Second |
Now you know how to change the format of date in your report. This article may help in changing the format to (year, month, day). This format is used in Japan, China, North Korea, South Korea, Taiwan, Hungary, Lithuania, and Iran; also, it is used as a collateral one in some European and Asian countries.