【问题标题】:Getting current Date by CultureInfo通过 CultureInfo 获取当前日期
【发布时间】:2016-10-08 04:01:46
【问题描述】:

我想在我的 Xamarin Forms 应用中获取本地化日期。我尝试了两种在 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 中的当前日期。

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

我什至通过SystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb["‌​Asia/Tehran"]).Date.‌​ToString() 尝试了 NodaTime,但日期仍然是默认文化!

更新

终于,Persian Calendar Plus 完成了任务!但是如果有人能弄清它,那将是非常有用的!

【问题讨论】:

  • 如果您在使用 DateTime 时遇到限制,了解 .Net 还有其他日期/时间库可能会有所帮助。斯基特大师的 NodaTime 是其中之一,但还有其他的。
  • @bubbleking,我正在使用它this way,我仍然得到en-USSystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb["Asia/Tehran"]).Date.ToString() 的日期。

标签: c# xamarin xamarin.forms portable-class-library .net-core


【解决方案1】:

.Net 框架不支持该文化的波斯日历。这种文化不接受日历;因此在这种文化中显示 DateTime 是不可能的。

您需要创建一个自定义的helper 或一些extension,如此处所述。

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

// 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 没有Persian Calendar了!
  • @Akbari 这就是我提到的
  • 这很糟糕,但感谢您的帮助。请您在回答中注意这与.Net Core 相关,以使其更完整,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-23
  • 2023-02-08
  • 2018-08-18
相关资源
最近更新 更多