日期时间解析问题(DateTime.ParseExact)
[请投票关闭此问题 - 请参阅我的最后一条评论。]
嗨,
类似这样的内容:
DateTime.ParseExact("25/12/2008 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
在我的开发计算机上工作正常,但在部署(服务器)后却不行。
我想这与某些时区配置有关。我已经尝试过了:
<%@ ... UICulture="en" Culture="en-US" %>
没有效果。有什么关于明信片的建议吗?谢谢。
Christian
PS:异常:
字符串未被识别为有效的日期时间
PPS:我已经更新了问题。我实际上是在喂时间。对此感到抱歉!
购买力平价: 我现在意识到这一切都与 Excel 和 oledb 有关。字符串 25/12/2008 在服务器上看起来像“12/25/2008 12:00:00 AM”,在开发计算机上看起来像“25/12/2008 00:00:00”。我将服务器时区调整为英国,但没有效果。我还能做什么?感谢并抱歉所有这些混乱!!!
[Please vote to close this - see my last comment.]
Hi,
Something like this:
DateTime.ParseExact("25/12/2008 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
works fine on my development machine but not after deployment (server).
I guess it has to do with some time zone configuration. I have tried:
<%@ ... UICulture="en" Culture="en-US" %>
with no avail. Any suggestions on a postcard please. Thanks.
Christian
PS: Exception:
String was not recognized as a valid DateTime
PPS: I have updated the question. I actually feed in the time bit. sorry about that!
PPPS:
I have now realised that all this has to do with Excel and oledb. The string 25/12/2008 looks like this "12/25/2008 12:00:00 AM" on the server and like this "25/12/2008 00:00:00" on the developement machine. I adjusted the time zone of the server to UK without avail. What else can I do? Thank and sorry about all this confusion!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您最好确切发布失败的内容和确切的错误,而不是“类似”失败的内容。
我希望您的示例给出 FormatException,因为您要转换的字符串(“25/12/2008”,没有时间)与指定的格式(“dd/MM/yyyy hh:mm:ss”)不匹配。
在格式中使用 hh 而不是 HH 也有点奇怪 - hh 是 12 小时制。
我希望以下任何一项都能发挥作用。
You'd do better posting exactly what failed, and the exact error, rather than "something like" what failed.
I would expect your sample to give a FormatException, since the string you're converting ("25/12/2008", no time) does not match the format specified ("dd/MM/yyyy hh:mm:ss").
Also a bit strange to be using hh rather than HH in your format - hh is a 12-hour clock.
I would expect any of the following to work.
将字符串解析为 DateTime 时,请始终考虑日期可能有一位或两位数字表示日和月。例如,它可以是 MM/dd/yyyy 或 M/d/yyyy 或 MM/d/yyyy 或 M/dd/yyyy。如果您使用 ParseExact 并且不考虑这一点,则会出现异常。试试这个:
When parsing a string to DateTime, always consider that the date could have one or two digits for day and month. For example, it could be MM/dd/yyyy or M/d/yyyy or MM/d/yyyy or M/dd/yyyy. If you use ParseExact and you don't consider that, you'll get an exception. Try this:
尝试
try