[英]C# Convert String to DateTime
i have string token[5] = Aug and string token[6]=1 (see on image 我有字符串标记[5] = 8月和字符串标记[6] = 1(请参见图片)
)
)
i want to convert to DateTime. 我想转换为DateTime。
i try: 我尝试:
DateTime DateCreated = DateTime.ParseExact(tokens[5] + tokens[6], "MM-dd", CultureInfo.InvariantCulture);
There are a few issues: 有几个问题:
tokens[5] + tokens[6]
. tokens[5] + tokens[6]
中缺少期望的连字符“-”。 MM
would expect a two-digit month, for a three letter shorthand month use MMM
. MM
希望使用两位数的月份,而对于三个字母的缩写月份,则可以使用MMM
。 dd
expects a two-digit day (so in your case, 01 instead of 1). dd
期望两位数的天(因此,在您的情况下为01而不是1)。 Use d
to take one- or two-digit days. d
表示一位或两位数天。 With all this said and done, you don't actually need the hyphen: 说完所有这些,您实际上不需要连字符:
var dateCreated = DateTime.ParseExact(tokens[5] + tokens[6], "MMMd", CultureInfo.InvariantCulture);
Refer to this MSDN entry for more options: 有关更多选项,请参考此MSDN条目:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx http://msdn.microsoft.com/zh-CN/library/8kb3ddd4.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.