16 April, 2010
0 Comments
1 category
If you want to convert a text containing a datetime in format “yyyyMMdd_HHmmss” to a datetime vaiable in C#, use the following code:
UnitTest
string dateTimeFormat = "yyyyMMdd_HHmmss"; string textContainingDateTime = string.Format("This is some text containing a datetime 20100808_080808 in the format [{0}]", dateTimeFormat); DateTime result = DateTime.Now; DateTime.TryParseExact(textContainingDateTime.Substring(40, dateTimeFormat.Length), dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out result); DateTime expectedResult = new DateTime(2010,8,8,8,8,8); Assert.AreEqual(expectedResult, result);
Result
Passed
Tags: C#
Category: Uncategorized