如何解析exception日期字符串
您好我有一个不寻常的日期格式,我想解析为DateTime对象
string date ="20101121"; // 2010-11-21 string time ="13:11:41: //HH:mm:ss
我想使用DateTime.Tryparse()
但我似乎无法开始这个。
谢谢你的帮助。
string date ="20101121"; // 2010-11-21 string time ="13:11:41"; //HH:mm:ss DateTime value; if (DateTime.TryParseExact( date + time, "yyyyMMddHH':'mm':'ss", new CultureInfo("en-US"), System.Globalization.DateTimeStyles.None, out value)) { Console.Write(value.ToString()); } else { Console.Write("Date parse failed!"); }
编辑:根据Frédéric的评论将时间分隔符标记用单引号括起来
您可以使用自定义格式的DateTime.TryParseExact()静态方法:
using System.Globalization; string date = "20101121"; // 2010-11-21 string time = "13:11:41"; // HH:mm:ss DateTime convertedDateTime; bool conversionSucceeded = DateTime.TryParseExact(date + time, "yyyyMMddHH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out convertedDateTime);
DateTime.TryParseExact()