Created
September 19, 2021 05:38
-
-
Save peymanmajidi/16893635a1a05796aeb0b97c6193dac7 to your computer and use it in GitHub Desktop.
Intilize persian culture means every usage of DateTime becomes to Hijri and there is no any concern and extra work needed. Simply print datetime in Hijri by `DateTime.Now` or in SQL Server Transactions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cultures | |
{ | |
// Call this method to initialize Persian Hijri culture | |
public static void InitPersian() | |
{ | |
InitCulture("fa-ir", new[] { "ی", "د", "س", "چ", "پ", "ج", "ش" }, | |
new[] { "یکشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" }, | |
new[] | |
{ | |
"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", | |
"بهمن", "اسفند", "" | |
}, | |
new[] | |
{ | |
"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", | |
"بهمن", "اسفند", "" | |
}, "صبح ", "عصر ", "yyyy/MM/dd", new PersianCalendar()); | |
} | |
public static void InitCulture(string culture, string[] abbreviatedDayNames, string[] dayNames, | |
string[] abbreviatedMonthNames, string[] monthNames, string amDesignator, | |
string pmDesignator, string shortDatePattern, Calendar calendar) | |
{ | |
var calture = new CultureInfo(culture); | |
var info = calture.DateTimeFormat; | |
info.AbbreviatedDayNames = abbreviatedDayNames; | |
info.DayNames = dayNames; | |
info.AbbreviatedMonthNames = abbreviatedMonthNames; | |
info.MonthNames = monthNames; | |
info.AMDesignator = amDesignator; | |
info.PMDesignator = pmDesignator; | |
info.ShortDatePattern = shortDatePattern; | |
info.FirstDayOfWeek = DayOfWeek.Saturday; | |
var cal = calendar; | |
var type = typeof(DateTimeFormatInfo); | |
var fieldInfo = type.GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); | |
if (fieldInfo != null) | |
fieldInfo.SetValue(info, cal); | |
var field = typeof(CultureInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); | |
if (field != null) | |
field.SetValue(calture, cal); | |
// ASP.NET MVC | |
Thread.CurrentThread.CurrentCulture = calture; | |
Thread.CurrentThread.CurrentUICulture = calture; | |
// ASP.NET CORE AND Higher | |
CultureInfo.CurrentCulture.DateTimeFormat = info; | |
CultureInfo.CurrentUICulture.DateTimeFormat = info; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment