I have a string value which is a datetime : "20100825161500" and I want to convert this to a System Datetime. I have tried Convert.ToDateTime and DateTime.Parse and these do not work.
+13
A:
You can use DateTime.ParseExact to pass the format you need.
Here is an example:
var parsed = DateTime.ParseExact("20100825161500","yyyyMMddHHmmss", null);
Possible format values are listed at Standard Date and Time Format Strings and Custom Date and Time Format Strings
Giorgi
2010-09-01 08:16:48
A:
Because this string hasn't a format recognized by these 2 functions.
DateTime.Parse and Convert.ToDateTime require your string to be correctly formatted : http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx
You will have to write your custom parser for this kind of conversion
Mike
2010-09-01 08:18:04
-1: DateTime.ParseExact allows a format to be specified, you don't need to write this yourself.
Richard
2010-09-01 08:18:56
I didn't know ParseExact. Thanks for the info
Mike
2010-09-01 08:26:25