[英]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."
在我的ASP.NET MVC应用程序中,ModelState始终无效,并显示以下错误消息:
"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
. 我正在尝试针对波斯文化对我的应用进行本地化,因此我在
web.config
中将uiCulture
和culture
设置为fa-IR
。 Still, DateTime.Now
doesn't return localized date. 不过,
DateTime.Now
不会返回本地化日期。
What's the correct way to globalize DateTime
? 全球化
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? 看来我应该将本地化版本存储在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.
您不能将PersianCalendar对象用作区域性的默认日历。 The default calendar is specified by the CultureInfo.Calendar property and must be one of the calendars returned by the CultureInfo.OptionalCalendars property.
默认日历由CultureInfo.Calendar属性指定,并且必须是CultureInfo.OptionalCalendars属性返回的日历之一。 Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.
当前,对于CultureInfo类支持的任何区域性,PersianCalendar类都不是可选日历,因此不能是默认日历。
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".
例如,您的日期可以这样表示:“ 2015-05-23T7:29:37Z”。
I also suggest you save and send your datetimes in UTC. 我还建议您保存并发送UTC时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.