Last active
October 11, 2016 13:02
-
-
Save Jalalx/5235490 to your computer and use it in GitHub Desktop.
Some persian extension methods
This file contains hidden or 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
// source code orginaly from http://www.dotnettips.info/post/1244/ | |
/* | |
Use as extension method: | |
var date1 = DateTime.Now.ToPesianDateString("yyyy/MM/dd"); | |
var date2 = DateTime.Now.ToPeianDateString("dddd, dd MMMM,yyyy"); | |
//Output: | |
//1391/12/13 | |
//یکشنبه, 13 اسفند,1391 | |
Use as default culture: | |
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = PersianDateExtensionMethods.GetPersianCulture(); | |
var d1 = DateTime.Now.ToString(); | |
//Output : 1391/12/13 11:25:44 ب.ظ | |
*/ | |
using System; | |
using System.Globalization; | |
using System.Reflection; | |
namespace System | |
{ | |
public static class PersianDateExtensionMethods | |
{ | |
private static CultureInfo _Culture; | |
public static CultureInfo GetPersianCulture() | |
{ | |
if (_Culture == null) | |
{ | |
_Culture = new CultureInfo("fa-IR"); | |
DateTimeFormatInfo formatInfo = _Culture.DateTimeFormat; | |
formatInfo.AbbreviatedDayNames = new[] { "ی", "د", "س", "چ", "پ", "ج", "ش" }; | |
formatInfo.DayNames = new[] { "یکشنبه", "دوشنبه", "سه شنبه", "چهار شنبه", "پنجشنبه", "جمعه", "شنبه" }; | |
var monthNames = new[] | |
{ | |
"فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", | |
"اسفند", | |
"" | |
}; | |
formatInfo.AbbreviatedMonthNames = | |
formatInfo.MonthNames = | |
formatInfo.MonthGenitiveNames = formatInfo.AbbreviatedMonthGenitiveNames = monthNames; | |
formatInfo.AMDesignator = "ق.ظ"; | |
formatInfo.PMDesignator = "ب.ظ"; | |
formatInfo.ShortDatePattern = "yyyy/MM/dd"; | |
formatInfo.LongDatePattern = "dddd, dd MMMM,yyyy"; | |
formatInfo.FirstDayOfWeek = DayOfWeek.Saturday; | |
System.Globalization.Calendar cal = new PersianCalendar(); | |
FieldInfo fieldInfo = _Culture.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance); | |
if (fieldInfo != null) | |
fieldInfo.SetValue(_Culture, cal); | |
FieldInfo info = formatInfo.GetType().GetField("calendar", BindingFlags.NonPublic | BindingFlags.Instance); | |
if (info != null) | |
info.SetValue(formatInfo, cal); | |
_Culture.NumberFormat.NumberDecimalSeparator = "/"; | |
_Culture.NumberFormat.DigitSubstitution = DigitShapes.NativeNational; | |
_Culture.NumberFormat.NumberNegativePattern = 0; | |
} | |
return _Culture; | |
} | |
public static string ToPesianDateString(this DateTime date,string format = "yyyy/MM/dd") | |
{ | |
return date.ToString(format,GetPersianCulture()); | |
} | |
} | |
} |
This file contains hidden or 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 static class PersianExtensions | |
{ | |
public static string FixPersianChars(this string Value) | |
{ | |
return Value.Replace('ی', 'ي').Replace("ک", "ك"); | |
} | |
public static string ToPersianNumber(this string input) | |
{ | |
if (String.IsNullOrEmpty(input)) | |
return ""; | |
//۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ | |
input = input.Replace("0", "۰"); | |
input = input.Replace("1", "۱"); | |
input = input.Replace("2", "۲"); | |
input = input.Replace("3", "۳"); | |
input = input.Replace("4", "۴"); | |
input = input.Replace("5", "۵"); | |
input = input.Replace("6", "۶"); | |
input = input.Replace("7", "۷"); | |
input = input.Replace("8", "۸"); | |
input = input.Replace("9", "۹"); | |
return String.Format(Format, input); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment