1

I'd like to get localized date in my Xamarin Forms app. I have tried two ways that work just fine in ASP.NET MVC, but I wonder why neither of them works in Xamarin.

var currentDate DateTime.Now.ToString("yyyy/mm/dd", new CultureInfo("fa-IR"))

And by using culture info's calendar:

var month = new CultureInfo("fa-IR").Calendar.GetMonth(DateTime.Now);

Both of these methods gave me the current date in en-US.

Apparently, PersianCalendar is supposed to be there. I can't understand what I'm missing?

I even tried NodaTime by, SystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb["‌​Asia/Tehran"]).Date.‌​ToString() and the date is still in default culture!

UPDATE

Finally, Persian Calendar Plus did the job! But it would be very useful if someone could get the bottom of it!

svick
  • 214,528
  • 47
  • 357
  • 477
Akbari
  • 2,239
  • 6
  • 36
  • 78
  • 1
    If you run into limitations using DateTime, it may be helpful to know that there are other date/time libraries out there for .Net. Grandmaster Skeet's NodaTime is one but there are others. – bubbleking Oct 08 '16 at 05:52
  • @bubbleking, I'm using it [this way](http://stackoverflow.com/a/27853841/3967440) and I still get `en-US` date by `SystemClock.Instance.Now.InZone(DateTimeZoneProviders.Tzdb["Asia/Tehran"]).Date.ToString()`. – Akbari Oct 08 '16 at 06:09

1 Answers1

1

.Net framework doesn’t support Persian calendar for the culture. This culture doesn’t accept the calendar; therefore display of DateTime in this culture is impossible.

You need to create a custom helper or some extension as mentioned here.

Using the above extension you can set like this,

// create an instance of culture
CultureInfo info = new CultureInfo(“fa-Ir”);
//set Persian calendar to it without get exception
info.DateTimeFormat.Calendar = new PersianCalendar();
Community
  • 1
  • 1
Sajeetharan
  • 186,121
  • 54
  • 283
  • 331