You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://github.com/shahabfar/Persia.Net/actions/workflows/publish.yml)
4
+
5
+
Persia.Net is a robust class library, meticulously designed to facilitate seamless conversion between Persian, Gregorian, and Arabic (Hijri) dates. Originally crafted with .NET, it has evolved to ensure compatibility with .NET 6.0, 7.0, and the latest 8.0, making it a versatile tool for a wide range of .NET applications.
You can also view the [package page](https://www.nuget.org/packages/Persia.Net/) on NuGet.
20
+
21
+
22
+
# Using Persia.Net
23
+
24
+
Persia.Net is a versatile class library designed to seamlessly convert dates between Persian, Gregorian, and Arabic (Hijri) calendars. This guide will show you how to use Persia.Net to convert dates.
25
+
26
+
## Converting Dates
27
+
28
+
You can convert both nullable and non-nullable `DateTime` objects to Persian dates using the `ToPersianDateTime()` extension method.
In this example, both nullableDate and nonNullableDate are converted to Persian dates.
40
+
41
+
### Accessing Date Components
42
+
After converting a date, you can access the day, month, and year components of the Persian date like this:
43
+
```csharp
44
+
intday=convertedNullableDate.Day;
45
+
intmonth=convertedNullableDate.Month;
46
+
intyear=convertedNullableDate.Year;
47
+
```
48
+
In this example, day, month, and year will hold the day, month, and year of the Persian date, respectively.
49
+
50
+
When you run this code, it will print:
51
+
```
52
+
Day: 20
53
+
Month: 12
54
+
Year: 1402
55
+
```
56
+
## PersianDateTime Class
57
+
58
+
The `PersianDateTime` class in the Persia.Net library is a comprehensive utility designed for handling and manipulating dates in the Persian calendar. Here's a brief overview of its capabilities:
59
+
60
+
| Property | Description |
61
+
| --- | --- |
62
+
|`Year`| Gets the year component of the date. |
63
+
|`Month`| Gets the month component of the date. |
64
+
|`Day`| Gets the day component of the date. |
65
+
|`Hour`| Gets the hour component of the time. |
66
+
|`Minute`| Gets the minute component of the time. |
67
+
|`Second`| Gets the second component of the time. |
68
+
|`Millisecond`| Gets the millisecond component of the time. |
69
+
|`Ticks`| Gets the number of ticks that represent the time. |
70
+
|`DayOfWeek`| Gets the day number of the week. This number is zero based. |
71
+
|`DaysInMonth`| Gets the days of the month. |
72
+
|`WeekOfMonth`| Gets the week number of the month. |
73
+
|`WeekOfYear`| Gets the week number of the year. |
74
+
|`MonthName`| Gets the name of the current month. |
75
+
|`DayOfWeekName`| Gets the day name of the week in the Persian Calendar. |
76
+
|`IsLeapYear`| Gets a value indicating whether the current Persian year is a leap year. |
77
+
|`DayOfYear`| Gets the number of days that have passed since the beginning of the current year in the Persian calendar. |
78
+
|`DaysRemainingInYear`| Gets the number of days remaining until the end of the current year in the Persian calendar. |
79
+
|`Now`| Gets the current date and time in the Persian calendar. |
80
+
|`UtcNow`| Gets the current date and time in the Persian calendar in Coordinated Universal Time (UTC). |
81
+
|`Today`| Gets the current date in the Persian calendar.|
82
+
83
+
84
+
### Converting Gregorian Date to Persian Date
85
+
86
+
The provided code snippet is a simple example of converting a Gregorian date to a Persian date using `ToPersianDateTime()` extension method.
87
+
88
+
```csharp
89
+
vardate=newDateTime(2024, 03, 10); // Define a Gregorian date
In addition to date conversion, the `PersianDateTime` class also includes built-in string generation methods. These methods transform the converted DateTime into a human-readable Persian string, providing a more intuitive representation of the date and time in Persian. The following table outlines these methods:
99
+
100
+
| Method | Description |
101
+
| --- | --- |
102
+
|`ToShortPersianString`| Converts the date to a string in the format "yyyy/MM/dd" with Persian numbers.<br>برای مثال, ۱۴۰۲/۱۲/۲۰|
103
+
|`ToPersianString`| Converts the date to a string in the Persian format with Year and Day in digit and Month in word.<br>برای مثال, ۲۰ اسفند ۱۴۰۲|
104
+
|`ToLongPersianString`| Converts the date to a string in the Persian format with Year and Day in digit and Month in word as well as weekday name and time.<br>برای مثال, یکشنبه ۲۰ اسفند ۱۴۰۲|
105
+
|`ToPersianDayMonthString`| Converts the date to a string in the Persian format with Year in digit and Day and Month in words.<br>برای مثال, بیستم اسفند ۱۴۰۲|
106
+
|`ToPersianWeekdayString`| Converts the date to a string in the Persian as well as week day name.<br>برای مثال, یکشنبه ۱۴۰۲/۱۲/۲۰|
107
+
|`ToLongPersianOrdinalWords`|Converts the date to a string in the Persian format with Year in digit and Day and Month in words as well as weekday name.<br>برای مثال, یکشنبه بیستم اسفند ۱۴۰۲|
108
+
109
+
## Converting Persian Date to Gregorian DateTime
110
+
In .NET, the `PersianDateTime` class provides straightforward methods for converting Persian dates to Gregorian dates or `DateTime`. Specifically, the `ToDateTime` and `ToDateOnly` methods can be utilized for this purpose. Both of these methods are available as static and instance methods within the `PersianDateTime` class. This allows for flexible usage depending on the specific requirements of your code.
111
+
112
+
The `ToDateTime` method in the `PersianDateTime` class allows you to convert a Persian date to a Gregorian `DateTime`. This method is static and takes three parameters: `year`, `month`, and `day`.
In the previous example, the time components (hour, minute, second, and millisecond) of the converted DateTime are derived from the system clock. However, if you wish to specify your own time values for the conversion, you can do so using the overloaded ToDateTime methods. These methods allow you to pass in the hour, minute, second, and optionally, the millisecond, along with the year, month, and day.
If you're only interested in the date portion of the converted `DateTime`, you can use the `ToDateOnly` method. This method returns a `DateOnly` object, which represents the converted Persian date without any time values.
147
+
148
+
Here's the method signature for your reference:
149
+
```csharp
150
+
DateOnlyToDateOnly(intyear, intmonth, intday)
151
+
```
152
+
153
+
### Human-Readable Time Difference Conversion
154
+
The `HumanizePersianDateTimePassed` method in the `PersianDateTime` class allows you to convert the time difference between the current date and a provided date into a human-readable format.
By default, this method generates a human-readable format with three components. However, if you prefer a more detailed output, you can specify up to six components. To do this, simply pass the desired number of components as an argument to this method.
163
+
164
+
Here's the example:
165
+
```csharp
166
+
// Define a DateTime
167
+
vardate=newDateTime(2023, 12, 21);
168
+
169
+
// Humanize the time difference between the current date and the provided date
// The result of the conversion is "۱۱ ماه و ۳۶۲ روز و ۲ ساعت پیش"
176
+
```
177
+
Additionally, if you prefer to display the elapsed time in terms of a single time unit, you can specify this as an argument to the method. For instance, if you want to express the elapsed time solely in terms of days, you can pass TimeUnit.Days as an argument to the method.
In this example, `HumanizePassedPersianDateTime` will return the elapsed time since the specified date, expressed solely in terms of days. This allows for a more focused representation of the elapsed time.
184
+
Here is the enumerable `TimeUnit` values:
185
+
186
+
```Years
187
+
Months
188
+
Days
189
+
Hours
190
+
Minutes
191
+
Seconds
192
+
```
193
+
### Retrieving Detailed Week and Month Data in the Persian Calendar
194
+
If you're interested in obtaining information about the current week or month in the Persian calendar, this library offers specific `DateTime` extension methods designed for this purpose. These methods include:
195
+
196
+
-`CurrentPersianWeek`: Retrieves the current week.
197
+
-`NextPersianWeek`: Retrieves the next week.
198
+
-`PreviousPersianWeek`: Retrieves the previous week.
199
+
-`CurrentPersianMonth`: Retrieves the current month.
200
+
-`NextPersianMonth`: Retrieves the next month.
201
+
-`PreviousPersianMonth`: Retrieves the previous month.
202
+
203
+
These methods provide a convenient way to navigate through the Persian calendar, allowing you to easily access and manipulate date-related data.
0 commit comments