开发者

C# Parse Date/Time with a unique format I haven't seen before

I'm trying to parse a date/time with a special format I haven't seen before...

2011-03-08-12.26.27.000000
2011-03-08-13.00.03.000000

Is there an easy function in C# that I'm missing so I c开发者_开发百科an convert to a date/time easily?

Thanks I really appreciate your help.


DateTime.ParseExact("2011-03-08-12.26.27.000000", "yyyy-MM-dd-HH.mm.ss.ffffff", null)

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx


This is what you are looking for:

DateTime.ParseExact("2011-03-08-12.26.27.000000", "yyyy-MM-dd-HH.mm.ss.ffffff", CultureInfo.InvariantCulture);

Thomas' answer is correct (mostly) but it won't work because of the error I mentioned as a response to his answer. Also I tried this using: "2011-03-23-13.00.00.000000" and it failed. hh needs to be HH.


You should use DateTime.ParseExact, code like this:

var dateString = "2011-03-08-12.26.27.000000";
var result = DateTime.ParseExact(dateString, "yyyy-MM-dd-HH.mm.ss.ffffff", CultureInfo.InvariantCulture);


Yes, you can use DateTime.ParseExact(dateString, "yyyy-MM-dd-HH.mm.ss.ffffff"). ParseExact requires the string to be in EXACTLY the format specified, but given that restriction it can handle any data format that can be expressed as a format string, which is no problem here.


Replace the third - with , then use DateTime.Parse


None of the answers above worked for me for the pattern described for some reason. So I resorted to using the TryParseExact like so:

string pattern = "yyyy-MM-dd-HH.mm.ss.ffffff";
DateTime parsedDate;

if (DateTime.TryParseExact(dmr.DMR2ODAT, pattern, null,
             DateTimeStyles.None, out parsedDate))
    ...

Which worked. Posting in case someone else runs into the same issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜