日期时间解析问题(DateTime.ParseExact)

发布于 2024-10-07 09:28:05 字数 656 浏览 3 评论 0原文

[请投票关闭此问题 - 请参阅我的最后一条评论。]

嗨,

类似这样的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-10-14 09:28:05

类似这样的事情

您最好确切发布失败的内容和确切的错误,而不是“类似”失败的内容。

我希望您的示例给出 FormatException,因为您要转换的字符串(“25/12/2008”,没有时间)与指定的格式(“dd/MM/yyyy hh:mm:ss”)不匹配。

在格式中使用 hh 而不是 HH 也有点奇怪 - hh 是 12 小时制。

我希望以下任何一项都能发挥作用。

// No time component
DateTime.ParseExact("25/12/2008", "dd/MM/yyyy", new CultureInfo("en-US"));

// Works for hours <=12, result is always AM
DateTime.ParseExact("25/12/2008 11:00:00", "dd/MM/yyyy hh:mm:ss", new CultureInfo("en-US"));

// Works for hours using 24-hour clock
DateTime.ParseExact("25/12/2008 13:00:00", "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US"));

Something like this

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.

// No time component
DateTime.ParseExact("25/12/2008", "dd/MM/yyyy", new CultureInfo("en-US"));

// Works for hours <=12, result is always AM
DateTime.ParseExact("25/12/2008 11:00:00", "dd/MM/yyyy hh:mm:ss", new CultureInfo("en-US"));

// Works for hours using 24-hour clock
DateTime.ParseExact("25/12/2008 13:00:00", "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US"));
伊面 2024-10-14 09:28:05

将字符串解析为 DateTime 时,请始终考虑日期可能有一位或两位数字表示日和月。例如,它可以是 MM/dd/yyyy 或 M/d/yyyy 或 MM/d/yyyy 或 M/dd/yyyy。如果您使用 ParseExact 并且不考虑这一点,则会出现异常。试试这个:

DateTime date = DateTime.ParseExact(
    dateText, // date in string 
    new string[] { "M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy" }, // formats (you can add more)
    CultureInfo.InvariantCulture, 
    DateTimeStyles.None);

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:

DateTime date = DateTime.ParseExact(
    dateText, // date in string 
    new string[] { "M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy" }, // formats (you can add more)
    CultureInfo.InvariantCulture, 
    DateTimeStyles.None);
寄与心 2024-10-14 09:28:05

尝试

DateTime.ParseExact("25/12/2008", "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);

try

DateTime.ParseExact("25/12/2008", "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文