DateTime全球化在ASP。NET MVC和SQL SERVER

时间:2022-11-09 03:30:35

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."

在我的ASP。NET MVC app, ModelState一直是无效的,有以下错误信息:“数值‘5/23/2015:7:29:37 AM’不适用注册日期。”

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.

我正在尝试本地化我的波斯文化应用,所以我在web.config中将uiCulture和culture设置为fa-IR。不过,DateTime。现在不返回本地化日期。

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

全球化DateTime的正确方式是什么?我应该将本地化日期存储在DB中吗?或者本地化版本只是为了视图?

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

看起来我应该在DB中存储本地化版本,我怎么才能在波斯文化中获得本地化版本呢?

UPDATE:

更新:

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

似乎我不能直接得到当前的波斯日期,我必须按照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.

您不能将一个PersianCalendar对象作为一个区域性的默认日历。默认日历由CultureInfo指定。Calendar属性,必须是CultureInfo返回的日历之一。OptionalCalendars财产。目前,PersianCalendar类不是CultureInfo类支持的任何区域性的可选日历,因此不能是默认日历。

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

但是如何知道哪个组件添加了错误消息,以及如何修复它?

1 个解决方案

#1


4  

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".

我建议您在客户端本地化您的日期,而不是在服务器或数据库端。这样,你就不会受到特定文化的束缚。例如,你的日期可以这样表达:“2015-05-23T7:29:37Z”。

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

我还建议您在UTC中保存并发送您的数据时间。

#1


4  

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".

我建议您在客户端本地化您的日期,而不是在服务器或数据库端。这样,你就不会受到特定文化的束缚。例如,你的日期可以这样表达:“2015-05-23T7:29:37Z”。

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

我还建议您在UTC中保存并发送您的数据时间。