转移到生产环境时的DateTime问题 [英] DateTime problems when moving to production environment

查看:94
本文介绍了转移到生产环境时的DateTime问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC应用程序中有几种表单,这些表单的日期值是通过datepicker工具选择的.这在本地调试时效果很好,但是在部署到云环境中时,我的日期未正确转换.我有以下代码:

I have several forms within my MVC app that have date values which are chosen via a datepicker tool. This works well when debugging locally but on deployment to the cloud environment my dates are not being correctly converted. I have the following code:

string[] uploaddate = form.GetValues("uploaddate");
string[] expirydate = form.GetValues("expirydate");

这将日期定为2013年8月31日,依此类推,从这里我将转换为DateTime,如下所示:

This gets the date as 31/08/2013 etc. and from here I was converting to DateTime as follows:

Convert.ToDateTime(uploaddate[0]);
Convert.ToDateTime(expirydate[0]);

当我部署到Azure服务器时,出现以下错误:

When I deploy to the Azure server I receive the following error:

字符串未被识别为有效的DateTime.

当我的应用程序以英国格式设计时,我认为实例映像具有美国文化.我该如何解决这个问题,以便无论用户的文化如何,都将信息保存到数据库中?

I think that the instance image has a culture of US while my application was designed in UK format. How can I get around this problem so that the information is saved to the database regardless of the culture of the user?

推荐答案

我将改进日期选择器,以便它回发的值始终为yyyy-MM-dd,但会显示您所关心的文化.这使问题成为客户端问题,而不是服务器问题(实际上是服务器问题)

I'd improve the date picker so that the value it posts back is always yyyy-MM-dd but displays in whichever culture you care about. This makes the problem a client problem, not a server one (which it is really)

更新

我已经做了一些调查,Convert.ToDateTime()只是使用当前的区域性设置调用了DateTime.Parse.我刚刚检查了我的一台VM,它以美国本地设置运行,这就是为什么您得到d/M/y错误的原因.我会标准化发送到服务器的日期的格式(这与哪种格式无关紧要,但我始终是YMD的支持者),然后使用@No提到的DateTime.TryParseExact(),但只有一种格式.

I've done some investigation Convert.ToDateTime() simply calls DateTime.Parse with the current culture settings. I've just checked one of my VMs and it runs with US local settings which is why your're getting the error you are with d/M/y. I would standarise the format of the date that is being sent to the server (it doesn't really matter to which format, but I'm always a fan of YMD) and then use DateTime.TryParseExact() that @No One mentions, but with just one format.

这篇关于转移到生产环境时的DateTime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆