/// <summary> /// nice method from persian calendar project by Nickmehr /// </summary> /// <param name="dateTime"></param> /// <param name="format">d(date short),D t(time short),T f(full),F /// - g,G , m,M y,Y and finally B (1393/07/18)</param> /// <returns></returns> public static string ToFa(DateTime?dateTime, string format) { if (!dateTime.HasValue) { return(string.Empty); } ShamsiDate sd = ToShamsiDate(dateTime.Value); if (format.Length == 1) { switch (format) { case "d": return(sd.ShortDate); //93/07/27 case "D": return(sd.LongDate); //یکشنبه, 27 مهر 1393 case "t": return(sd.ShortTime); case "T": return(sd.LongTime); case "f": //Long date + short time return(string.Format("{0} {1}", sd.LongDate, sd.ShortTime)); case "F": // Long date + long time //یکشنبه, 27 مهر 1393 01:15:43 return(string.Format("{0} {1}", sd.LongDate, sd.LongTime)); case "g": //Short date + short time //93/07/27 01:14:24 return(string.Format("{0} {1}", sd.ShortDate, sd.ShortTime)); case "G": //Short date + long time return(string.Format("{0} {1}", sd.ShortDate, sd.LongTime)); case "m": case "M": //Month and day return(string.Format("{0} {1}", sd.MahName, sd.RoozeMah)); case "y": case "Y": // year and month return(string.Format("{0} {1}", sd.Saal, sd.MahName)); case "B": //best with year and month and day ,simple return(string.Format("{0}/{1:00}/{2:00}", sd.Saal, sd.Mah, sd.RoozeMah)); default: return(sd.ShortDate); } } else { //important: first replace longer occurances format = format.Replace("YY", "yy"); return(format .Replace("yyyy", sd.Saal.ToString(CultureInfo.InvariantCulture)) .Replace("yy", sd.Saal.ToString(CultureInfo.InvariantCulture).Substring(2, 2)) .Replace("MMM", sd.MahName.ToString(CultureInfo.InvariantCulture)) .Replace("MM", sd.Mah.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("M", sd.Mah.ToString(CultureInfo.InvariantCulture)) .Replace("ddd", sd.RoozeHaftehName.ToString(CultureInfo.InvariantCulture)) .Replace("dd", sd.RoozeMah.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("d", sd.RoozeMah.ToString(CultureInfo.InvariantCulture)) .Replace("hh", sd.Saat.ToString(CultureInfo.InvariantCulture)) .Replace("mm", sd.Daghighe.ToString(CultureInfo.InvariantCulture)) .Replace("ss", sd.Saniyeh.ToString(CultureInfo.InvariantCulture))); } }
/// <summary> /// nice method from persian calendar project by Nickmehr /// </summary> /// <param name="dateTime"></param> /// <param name="format">d(date short),D t(time short),T f(full),F /// - g,G , m,M y,Y and finally B (1393/07/18)</param> /// <returns></returns> public static string ToFa(this DateTime dateTime, string format = "B") { ShamsiDate sd = ToShamsiDate(dateTime); if (format.Length == 1) { switch (format) { case "d": return(sd.ShortDate); //93/07/27 case "D": return(sd.LongDate); //یکشنبه, 27 مهر 1393 case "t": return(sd.ShortTime); case "T": return(sd.LongTime); case "f": //short date + short time format = ("YYYY/MM/dd hh:mm"); break; case "F": // Long date + long time //یکشنبه, 27 مهر 1393 01:15:43 return(string.Format("{0} ساعت {1}", sd.LongDate, sd.LongTime)); case "g": //Short date + short time //93/07/27 01:14:24 return(string.Format("{0} {1}", sd.ShortDate, sd.ShortTime)); case "G": //Short date + long time return(string.Format("{0} {1}", sd.ShortDate, sd.LongTime)); case "m": return(string.Format("{0} {1}", sd.RoozeMah, sd.MahName)); case "M": //Month and day return(string.Format("{0} {1}", ShamsiDays[sd.RoozeMah - 1] ?? "", sd.MahName)); case "y": case "Y": // year and month return(string.Format("{0} {1}", sd.Saal, sd.MahName)); case "B": //best with year and month and day ,simple return(string.Format("{0}/{1:00}/{2:00}", sd.Saal, sd.Mah, sd.RoozeMah)); default: return(sd.ShortDate); } } //important: first replace longer occurances return(format.Replace("YY", "yy") // because year is not case sensetive .Replace("yyyy", sd.Saal.ToString(CultureInfo.InvariantCulture)) .Replace("yy", sd.Saal.ToString(CultureInfo.InvariantCulture).Substring(2, 2)) .Replace("MMM", sd.MahName.ToString(CultureInfo.InvariantCulture)) .Replace("MM", sd.Mah.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("M", sd.Mah.ToString(CultureInfo.InvariantCulture)) .Replace("D", ShamsiDays[sd.RoozeMah - 1]?.ToString(CultureInfo.InvariantCulture)) .Replace("ddd", sd.RoozeHaftehName.ToString(CultureInfo.InvariantCulture)) .Replace("dd", sd.RoozeMah.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("d", sd.RoozeMah.ToString(CultureInfo.InvariantCulture)) .Replace("hh", sd.Saat.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("mm", sd.Daghighe.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')) .Replace("ss", sd.Saniyeh.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'))); }