C# 通过CultureInfo获取当前日期

C# 通过CultureInfo获取当前日期,c#,xamarin,xamarin.forms,portable-class-library,.net-core,C#,Xamarin,Xamarin.forms,Portable Class Library,.net Core,我想在我的Xamarin表单应用程序中获取本地化日期。我尝试了两种在ASP.NET MVC中运行良好的方法,但我想知道为什么这两种方法在Xamarin中都不起作用 var currentDate DateTime.Now.ToString("yyyy/mm/dd", new CultureInfo("fa-IR")) 通过使用文化信息的日历: var month = new CultureInfo("fa-IR").Calendar.GetMonth(DateTime.Now); 这两种方法

我想在我的Xamarin表单应用程序中获取本地化日期。我尝试了两种在ASP.NET MVC中运行良好的方法,但我想知道为什么这两种方法在Xamarin中都不起作用

var currentDate DateTime.Now.ToString("yyyy/mm/dd", new CultureInfo("fa-IR"))
通过使用文化信息的日历:

var month = new CultureInfo("fa-IR").Calendar.GetMonth(DateTime.Now);
这两种方法都为我提供了
en-US
中的当前日期

显然,
PersianCalendar
将在那里。我不明白我错过了什么

我甚至尝试了NodaTime by,
SystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb[”‌​亚洲/德黑兰“])。日期。‌​ToString()
并且日期仍处于默认区域性中

更新


最后,我完成了任务!但是,如果有人能弄清真相,那将是非常有用的

.Net framework不支持区域性的波斯日历。这种文化不接受日历;因此,在此区域性中显示日期时间是不可能的

您需要创建一个自定义或此处提到的一些

使用上面的扩展,您可以这样设置

// create an instance of culture
CultureInfo info = new CultureInfo(“fa-Ir”);
//set Persian calendar to it without get exception
info.DateTimeFormat.Calendar = new PersianCalendar();

.Net
确实支持
fa-IR
文化,我正在
MVC
应用程序中使用它,我是不是遗漏了什么?@Akbari你有没有像上面那样尝试过?看来
.Net-core
已经没有了@阿克巴里这就是我提到的那很糟糕,但是谢谢你的帮助。请您在回答中注意,这与
.Net Core
有关。为了使其更完整,如果您在使用DateTime时遇到限制,了解.Net还有其他日期/时间库可能会有所帮助。Skeet大师的NodaTime是一个,但还有其他的。@bubbleking,我正在使用它,我仍然通过
SystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb[“Asia/Tehran”]).date.ToString()获得
en US
日期。