How to create a Persian calendar in FastReport?

Time:2021-10-16

FastReport. Net is a full-featured reporting solution for Windows Forms, asp.net, MVC and. Net core. It can be used in Microsoft Visual Studio 2005-2019. Support. Net framework 2.0-4. X,. Net core 3.0 and above.

In the new version of FastReport. Net 2021.3, hidpi support is implemented in the demo application. Razor components for embedding razor server applications have been added. Xodt and xods have new exports. The FastReport opensource version can now import devaexpress, list, label, and SQL Server Reporting Services reports. (click the button below to download)

Do you know that different countries have different date formats? When you are making multilingual reports or reporting for countries where they speak Persian (Persian), it is important to display the dates in the correct format. By default, FastReport uses the European date format, but. Net tools allow it to be converted to different formats.

Therefore, our task is to convert the date to Persian format. For example, our report has an expression [date], which represents today’s date:

[æ¥æ]

The expression [date] obtains the current system date in datetime format, but the value of all processed text reports is string. Let’s create a new function in the report script:

private void ConvertToPersianDate(object sender, EventArgs e)
1.
We will invoke this function from the text object. We create a temporary variable that converts the text of the object to www.sangpi.comdatetime:

DateTime d = DateTime.Parse((sender as TextObject).Text);
1.
“Sender as TextObject” is the address of the object calling the function. If we deal with it in this way, we can use its functions and properties.

After that, we will need the personacalendar object, which converts the date to Persian format:

PersianCalendar pc = new PersianCalendar ( ) ;
1.
Note that this object is stored in the system. Globalization library and must be indicated in the use section.

Then we have to change the text of the object. Consider the game line in more detail:

(sender as TextObject).Text = string.Format(“{0}/{1}/{2}”, pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d));
1.
Here we set the text of the object. The format of this value is year / month / day, because we use the function of personacalendar to get the corresponding value. You can edit the code portion of the setup text as needed. For example, the date code in the format of day.month.year is as follows:

“{0}.{1}.{2}”, pc.GetDayOfMonth(d), pc.GetMonth(d), pc.GetYear(d)
1.
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 related object.

åæ°æ®

Now the date looks like this:

æåç»æ

Refer to the following table for other time related functions:

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 date format in the report. This article may help to change the format to (year, month, day). This format is used in Japan, China, North Korea, South Korea, Taiwan, Hungary, Lithuania and Iran; In addition, it is used as collateral in some European and Asian countries.