转换YYYYMMDDHHMMSS的字符串值格式为C#的DateTime [英] Convert String value format of YYYYMMDDHHMMSS to C# DateTime
问题描述
我需要一个字符串值,其形式为YYYYMMDDHHMMSS为DateTime转换。但不知道如何,可能是一个DateTime.Tryparse可以用来实现这一目标。或者有没有其他的办法做到这一点。我可以使用一些字符串操作采取年月日独,转换为datetime,然后分别添加HH,MM,SS到日期时间做到这一点。但是它有什么DateTime.TryParse()方法,我可以在一个行中使用一个YYYYMMDDHHMMSS格式字符串值转换为DateTime值?
I have a need to convert a string value in the form "YYYYMMDDHHMMSS" to a DateTime. But not sure on how, may be a DateTime.Tryparse can be used to make this happen. Or is there any other way to do it. I can do this using some string operations to take "YYYYMMDD" alone, convert to a datetime and then add HH, MM, SS separately to that DateTime. But is there any DateTime.TryParse() methods that I can use in one line to convert a "YYYYMMDDHHMMSS" format string value to a DateTime value?
推荐答案
定义你自己的解析格式字符串使用。
Define your own parse format string to use.
string formatString = "yyyyMMddHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
在情况下,你有有毫秒的日期时间,请使用以下的formatString
In case you got a datetime having milliseconds, use the following formatString
string format = "yyyyMMddHHmmssfff"
string dateTime = "20140123205803252";
DateTime.ParseExact(dateTime ,format,CultureInfo.InvariantCulture);
感谢
这篇关于转换YYYYMMDDHHMMSS的字符串值格式为C#的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!