How convert Gregorian date to Persian date?

安稳与你 提交于 2019-11-29 04:35:49

问题


I want to convert a custom Gregorian date to Persian date in C#. For example, i have a string with this contents:

string GregorianDate = "Thursday, October 24, 2013";

Now i want to have:

string PersianDate = پنج‌شنبه 2 آبان 1392 ;

or

string PersianDate = 1392/08/02

Thanks


回答1:


Use the PersianCalendar:

string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));



回答2:


You can use PersianDateTime:

PM> Install-Package PersianDateTime

The Reference: PersianDateTime

You can use string.Split() if you need customization.




回答3:


    DateTime date = new DateTime(2013, 10, 24);
    var calendar = new PersianCalendar();
    var persianDate = new DateTime(calendar.GetYear(date), calendar.GetMonth(date), calendar.GetDayOfMonth(date));
    var result = persianDate.ToString("yyyy MMM ddd", CultureInfo.GetCultureInfo("fa-Ir"));



回答4:


Adding up to other answers, You can get the first one by using PersianDateTime:

var gregorianDate = "Thursday, October 24, 2013";
var date = DateTime.Parse(gregorianDate);
var persianDate = new PersianDateTime(date);
var result = persianDate.ToString("dddd d MMMM yyyy");



回答5:


In windows 10, using framework 4+:

Console.WriteLine(Convert.ToDateTime("1392/08/02",new CultureInfo("fa-IR")));

or

Console.WriteLine(DateTime.Parse("1392/08/02", new CultureInfo("fa-IR")));



回答6:


I suggest using MD.PersianDateTime, you can use it as easy as DateTime:

var persianDateTimeNow = PersianDateTime.Now;
persianDateTimeNow.EnglishNumber = true;
Console.WriteLine(persianDateTimeNow.ToString());

https://github.com/Mds92/MD.PersianDateTime



来源:https://stackoverflow.com/questions/30683664/how-convert-gregorian-date-to-persian-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!