如何解决在C中使用Window XP时的日期格式问题?
我使用的是 Window 7,当我在本地运行代码时一切正常,但是当上传到 Window server 2003 时代码会出现错误:
“字符串未被识别为有效的日期时间”
我的计算机设置(日期格式)与我的窗口服务器相同。仍然出现同样的问题。
下面是我的代码:
dateFrom = Convert.ToDateTime("12-6-2011");
dateTo = Convert.ToDateTime("18-6-2011");
//or
dateFrom = DateTime.ParseExact("12-6-2011", "MM/dd/yyyy", CultureInfo.InvariantCulture);
dateTo = DateTime.ParseExact("18-6-2011", "MM/dd/yyyy", CultureInfo.InvariantCulture);
//i using parse also din come out.
I am using Window 7, when I run my code in local everything is alright, but when upload to Window server 2003 the code will coming error:
"String was not recognized as a valid DateTime"
My computer setting (date format) all same with my window server. Still come out the same problem.
below is my code:
dateFrom = Convert.ToDateTime("12-6-2011");
dateTo = Convert.ToDateTime("18-6-2011");
//or
dateFrom = DateTime.ParseExact("12-6-2011", "MM/dd/yyyy", CultureInfo.InvariantCulture);
dateTo = DateTime.ParseExact("18-6-2011", "MM/dd/yyyy", CultureInfo.InvariantCulture);
//i using parse also din come out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道这与“C”语言有什么关系,所以我可能误解了一些东西。但是,据我所知,您使用了错误的格式字符串。
如果您的日期总是这样,请尝试使用“dM-yyyy”之类的格式字符串。
请查看以下页面以供参考: http://msdn.microsoft.com /en-us/library/8kb3ddd4.aspx
I do not know how this relates to the 'C' language, so I may have misinterpreted something. However, you are using the wrong format string as far as I can see.
If your dates always look like that, try using a format string like "d-M-yyyy".
Have a look at the following page for reference: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
MM 代表月份。您应该使用 dd/MM/yyyy 来拟合您的数据。
MM stands for month. You shoud use dd/MM/yyyy to fit your data.
尝试:
Try: