.net DateTime.Parse DD/MM/YYYY 24 小时制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10325813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime.Parse DD/MM/YYYY 24 Hour Clock
提问by paparazzo
DateTime.Parse fails on
DateTime.Parse 失败
15/08/2000 16:58
Any thoughts?
有什么想法吗?
I need to parse dates and get some international.
我需要解析日期并获得一些国际。
DD/MM/YYYY will not flop to 24 hour clock
DD/MM/YYYY 不会跳到 24 小时制
MM/DD/YYYY will accept 24 hour clock
MM/DD/YYYY 将接受 24 小时制
15/08/2000 4:58 PM will parse
15/08/2000 4:58 PM 将解析
From the Kibbee answer I looked at using other cultures. I use Regex to determine if it is dd/MM and if so use culture fr-FR.
从 Kibbee 的回答中,我看到了使用其他文化。我使用正则表达式来确定它是否是 dd/MM,如果是,则使用文化 fr-FR。
回答by Justin Niessner
var result = DateTime.ParseExact(dateString,
"dd/MM/yyyy HH:mm",
new CultureInfo("en-US"));
回答by Kibbee
You should probably be using DateTime.ParseExactto parse the date if you know the exact format that you expect the date to be in. For your purposes, the following will probably work.
如果您知道您希望日期采用的确切格式,您可能应该使用DateTime.ParseExact来解析日期。出于您的目的,以下内容可能会起作用。
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "15/08/2000 16:58"
format = "dd/MM/yyyy HH:mm"
result = DateTime.ParseExact(dateString, format, provider);
Change to above. Changed hh to HH because HH signifies 24 hour time. If you don't use a leading zero, then simply use H. For more information on creating format strings see this article.
换上上面的。将 hh 更改为 HH,因为 HH 表示 24 小时时间。如果不使用前导零,则只需使用 H。有关创建格式字符串的更多信息,请参阅此文章。
Also from the linked MSDN article, it appears as though the format "g" should work.
同样从链接的 MSDN 文章中,似乎格式“g”应该有效。
dateString = "15/06/2008 08:30";
format = "g";
CultureInfo provider = new CultureInfo("fr-FR");
DateTime result = DateTime.ParseExact(dateString, format, provider);

