在英国开发服务器与美国实时服务器上保存日期时间对象
我有一个网站将模型中的日期保存到数据库中。
启动我的网站后,我意识到服务器具有美国区域设置,因此我必须确保公共网站上的所有日期均采用英国格式。
但是现在,当我转到“创建”页面并选择一个日期并单击“保存”时,我收到错误值“22/11/2009”对于“日期”字段无效。必须出现该错误来自我的模型上的默认 DataAnnotations。
我可以做些什么来让它接受英国格式的日期吗?
I have a site that saves a date in my model to the database.
After launching my site I realised that the server has US regional settings so I had to make sure that all date's on the public site were formatted in the UK format.
However now when I go to my 'Create' page and choose a date and click save I get the error The value '22/11/2009' is not valid for the Date field. which must be coming from the default DataAnnotations on my model.
Is there something I can do to get it to accept UK formatted dates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设法通过将其放入 web.config 来使其工作
I managed to get it working by putting this into the web.config
你试过这个吗?如果此站点仅在英国使用,您可以将其放在 Global.asax Application_Init 中。如果是基于用户的,可以放在Application_BeginRequest中。这将为应用程序中的所有日期和数字提供默认格式和解析。
Have you tried this? If this site is only used in the UK, you can probably put this in the Global.asax Application_Init. If it is based on the user, you can put it in Application_BeginRequest. This will provide the default formatting and parsing for all dates and numbers in the application.
执行 SO 所做的操作:保留 UTC 中的所有日期,并将它们呈现为 UCT,并为 GMT/UTC/Zulu 附加“Z”。它可能并不理想,但有时简单性带来的收益超过了替代方案的成本。想想看。
Do what SO does: persist all dates in UTC and render them as UCT with an appended "Z" for GMT/UTC/Zulu. It might not be ideal but sometimes the gain from simplicity outweighs the costs of the alternative. Think about that.