Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use DateTime.Parse() to create a DateTime object

If I have a string that is in the format yyyyMMddHHmmssfff for example 20110815174346225. how would I create a DateTime object from that String. I tried the following

DateTime TimeStamp = DateTime.Parse(Data[1], "yyyyMMddHHmmssfff");

However I get these errors:

Error   1   The best overloaded method match for 'System.DateTime.Parse(string, System.IFormatProvider)' has some invalid arguments C:\Documents and Settings\rkelly1\Desktop\sd\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 67  29  WindowsFormsApplication1


Error   2   Argument 2: cannot convert from 'string' to 'System.IFormatProvider'    C:\Documents and Settings\rkelly1\Desktop\sd\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 67  53  WindowsFormsApplication1
like image 389
Richard Avatar asked Aug 15 '11 18:08

Richard


3 Answers

var sDate = "20110815174346225";
var oDate = DateTime.ParseExact(sDate, "yyyyMMddHHmmssfff", CultureInfo.CurrentCulture);
like image 120
Juan Ayala Avatar answered Nov 13 '22 01:11

Juan Ayala


You would have to use

DateTime time = DateTime.ParseExact(String,String, IFormatProvider);

The first argument string is going to be your date. The second argument string is going to be your format The third argument is your culture info (which is the IFormatProvider

So you would have

DateTime TimeStamp = DateTime.ParseExact(Data[1],"yyyyMMddHHmmssfff",CultureInfo.InvariantCulture);

The culture info is "A CultureInfo object that represents the culture used to interpret s. The DateTimeFormatInfo object returned by its DateTimeFormat property defines the symbols and formatting in s." From MSDN.

here's the link for more info. http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx

like image 38
Kevin Avatar answered Nov 13 '22 00:11

Kevin


Use DateTime.ParseExact:

DateTime dateTime = DateTime.ParseExact("[Your Date Here]",
                                        "yyyyMMddHHmmssfff",  
                                        CultureInfo.InvariantCulture);

Here's the MSDN Docs.

like image 30
James Hill Avatar answered Nov 13 '22 01:11

James Hill