简体   繁体   English

C#中的日期和时间转换 - DateTime.ParseExact()无法按预期工作

[英]Date and time conversion in C# - DateTime.ParseExact() not working as expected

I have date/time format, for example: "1-Mar-13 92230" According to this document and this link the format is as follows: "d-MMM-yy Hmmss", because: 我有日期/时间格式,例如:“1-Mar-13 92230”根据此文件此链接 ,格式如下:“d-MMM-yy Hmmss”,因为:

Day is single digit, 1-30
Month is 3 letter abbreviation, Jan/Mar etc.
Year is 2 digits, eg 12/13
Hour is single digit for 24 hour clock, eg 9, 13 etc. (no 09)
Minute is standard (eg 01, 52)
Second is standard (eg 30, 02)

I'm trying to run the following code in my program, but I keep getting an error of "String was not recognized as a valid DateTime." 我正在尝试在我的程序中运行以下代码,但我不断收到“String未被识别为有效DateTime”的错误。

string input = "1-Mar-13 92330";
        var date = DateTime.ParseExact(input, "d-MMM-yy Hmmss", 
System.Globalization.CultureInfo.CurrentCulture);

Please help, I'm not too familiar with DateTime conversions, but I can't see where I've gone wrong here. 请帮助,我对DateTime转换不太熟悉,但我看不出我在哪里出错了。 Thanks! 谢谢!

UPDATE: Is this because time cannot be parsed without colons in between? 更新:这是因为没有冒号之间的时间无法解析? (eg 1-Mar-13 9:22:30 gets parsed, but i have an external data source that would be impossible to rewrite from Hmmss to H:mm:ss) (例如1-Mar-13 9:22:30被解析,但我有一个外部数据源,无法从Hmmss重写为H:mm:ss)

You could fix your date: 你可以修改你的约会日期:

var parts = "1-Mar-13 92230".Split(' ');

if (parts[1].Length == 5)
{
    parts[1] = "0" + parts[1];
}

var newDate = parts[0] + " " + parts[1];

var date = DateTime.ParseExact(newDate, "d-MMM-yy HHmmss",  System.Globalization.CultureInfo.CurrentCulture);

From msdn : 来自msdn

If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. 如果format是不包含日期或时间分隔符的自定义格式模式(例如“yyyyMMdd HHmm”),请使用provider参数的不变区域性和每个自定义格式说明符的最宽格式。 For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H". 例如,如果要在格式模式中指定小时数,请指定更宽的形式“HH”,而不是更窄的形式“H”。

so you can try: 所以你可以尝试:

string input = "1-Mar-13 92330";
        var date = DateTime.ParseExact(input, "d-MMM-yy Hmmss", 
System.Globalization.CultureInfo.InvariantCulture);

Your input string has to be of following format 您的输入字符串必须是以下格式

string input = "1-Mar-13 092330";

If you go back to your link, it says 如果你回到你的链接,它说

H   24-hour clock hour (e.g. 19)

Now H is 24 hours, it should be represented with leading 0. If not imagine how would you handle the case of hours greater than 9 ie which are in double digit. 现在H是24小时,它应该以前导0表示。如果不能想象你将如何处理小于9的小时数,即两位数的情况。

If not your hour and Minute and Seconds has to be separated. 如果不是,你的小时和分钟和秒必须分开。

string input = "1-Mar-13 9 2330";

var date = DateTime.ParseExact(input, "d-MMM-yy H mmss", 
System.Globalization.CultureInfo.InvariantCulture);

Your hour min and seconds need to be separated, as they are not getting distinguished. 你的小时分和秒需要分开,因为它们没有得到区分。

string input = "1-Mar-13 9 23 30";
        var date = DateTime.ParseExact(input, "d-MMM-yy H mm ss",   System.Globalization.CultureInfo.CurrentCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM