简体   繁体   中英

DateTime Globalization in ASP.NET MVC and SQL SERVER

In my ASP.NET MVC app, ModelState is always invalid with the following error message: "The value '5/23/2015 7:29:37 AM' is not valid for Register Date."

I'm trying to localize my app for Persian culture, so I set uiCulture and culture to fa-IR in the web.config . Still, DateTime.Now doesn't return localized date.

What's the correct way to globalize DateTime ? Should I store localized date in DB? Or the localized version is just for views?

It seems I'm supposed to store localized version in DB, how can I get that for Persian culture?

UPDATE:

It seems I can't get current Persian date directly and I have to convert it as stated in MSDN :

You cannot use a PersianCalendar object as the default calendar for a culture. The default calendar is specified by the CultureInfo.Calendar property and must be one of the calendars returned by the CultureInfo.OptionalCalendars property. Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.

Bu how can I know which component is adding the error message and how can I fix it?

I suggest you localize your date on the client side, and not on the server or database side. This way, you're never bound to a specific culture. For instance, your date could be expressed this way : "2015-05-23T7:29:37Z".

I also suggest you save and send your datetimes in UTC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM