Method One: Convert.todatetime (String)
String format is required and must be YYYY-MM-DD HH:MM:SS
================================================
Method Two: Convert.todatetime (String, IFormatProvider)
DateTime DT;
DateTimeFormatInfo Dtformat = new System.globalizationdatetimeformatinfo ();
Dtformat.shortdatepattern = "YYYY/MM/DD";
DT = Convert.todatetime ("2011/05/26", Dtformat);
================================================
Method Three: DateTime.ParseExact ()
String datestring = "20110526";
DateTime dt = DateTime.ParseExact (datestring, "YyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Or
DateTime dt = DateTime.ParseExact (datestring, "YyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Attached reference information:
CultureInfo CultureInfo = cultureinfo.createspecificculture ("en-us");
string format = "ddd MMM d HH:mm:ss zz00 yyyy";
String stringvalue = DateTime.Now.ToString (format, cultureInfo); Get date string
datetime = DateTime.ParseExact ("Wed Aug 16:28:03 +0800", format, cultureInfo);//convert String to own Period
Other
Date format: YyyyMMdd HH:mm:ss (Note that this string is very strict letter case) YYYY: representative of the year mm: representative of the month dd: representative days hh: representative hours (24 hours) mm: representative minutes SS:&N Bsp Represents the second DateTime.Now.ToShortTimeString () DateTime dt = datetime.now; dt. ToString ();//2005-11-5 13:21:25 dt. Tofiletime (). ToString ();//127756416859912816 dt. TOFILETIMEUTC (). ToString ();//127756704859912816 dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25 dt. Tolongdatestring (). ToString ();//November 5, 2005 DT. Tolongtimestring (). ToString ();//13:21:25 dt. ToOADate (). ToString ();//38661.5565508218 dt. ToShortDateString (). ToString ();//2005-11-5 dt. Toshorttimestring (). ToString ();//13:21 dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25 dt. Year.tostring ();//2005 dt. Date.tostring ();//2005-11-5 0:00:00 dt. Dayofweek.tostring ();//saturday dt. Dayofyear.tostring ();//309 dt. Hour.tostring ();//13 dt.Millisecond.ToString ();//441 dt. Minute.tostring ();//30 dt. Month.tostring ();//11&nBsp Dt. Second.tostring ();//28 dt. Ticks.tostring ();//632667942284412864 dt. Timeofday.tostring ();//13:30:28.4412864 dt. ToString ();//2005-11-5 13:47:04 dt. Addyears (1). ToString ();//2006-11-5 13:47:04 dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04 dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04 dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04 dt. Addmonths (1). ToString ();//2005-12-5 13:47:04 dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05 dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10 dt. Addticks (1000). ToString ();//2005-11-5 13:47:04 dt.compareto (DT). ToString ();//0 dt. ADD (?). ToString ()//question mark is a time period dt. Equals ("2005-11-6 16:11:04"). ToString ();//false dt. Equals (DT). ToString ();//true dt. GetHashCode (). ToString ();//1474088234 dt. GetType (). ToString ();//system.datetime dt. GetTypeCode (). ToString ();//datetime
Dt.getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25 dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06 dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005 DT. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005 DT. Getdatetimeformats (' D ') [1]. ToString ();//2005 05 dt. Getdatetimeformats (' D ') [2]. ToString ();//week 62,005 05 dt. Getdatetimeformats (' D ') [3]. ToString ()//Saturday November 5, 2005 DT. Getdatetimeformats (' M ') [0]. ToString ();//November 5 dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06 dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06 dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, Nov 14:06:25 gmt string. Format ("{0:d}", dt);//2005-11-5 string. Format ("{0}", dt);//November 5, 2005 string. Format ("{0:f}", dt);//November 5, 2005 14:23 string. Format ("{0:f}", dt);//November 5, 2005 14:23:23 string. Format ("{0:g}", DT);//2005-11-5 14:23 string. Format ("{0:g}", DT);//2005-11-5 14:23:23 string. ForMat ("{0:m}", dt);//November 5 string. Format ("{0:r}", DT);//sat, Nov 14:23:23 gmt String.Format ("{0:s}", dt);//2005-11-05t14:23:23 string . Format ("{0:t}", dt);//14:23 string. Format ("{0:t}", dt);//14:23:23 string. Format ("{0:u}", DT);//2005-11-05 14:23:23z string. Format ("{0:u}", dt);//November 5, 2005 6:23:23 string. Format ("{0:y}", dt);//November 2005 string. Format ("{0}", dt);//2005-11-5 14:23:23 string. Format ("{0:yyyymmddhhmmssffff}", DT); calculates the number of days difference between 2 dates ----------------------------------------------- DateTime DT1 = Convert.datetime ("2007-8-1"); datetime DT2 = Convert.datetime ("2007-8-15"); span = DT2. Subtract (DT1); int daydiff = span. Days + 1; calculates a month of a certain year ----------------------------------------------- int day = Datetime.daysinmonth ( 2007, 8; days = 31; Add day to date, reduce day ----------------------------------------------- DateTime dt = datetime.now; dt. AddDays (1); Add a day &NBSp Dt. AddDays (-1); Reduce day other year methods similar to ... Oracle SQL convert date function ----------------------------------------------- To_date ("2007-6-6", "Yyyy-mm-dd"); to_date ("2007/6/6", ' yyyy/mm/dd ');