This article mainly describes the C # string format date time string into a DateTime type method, the need for friends can refer to the following
Method One: Convert.todatetime (String)
The 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 ();d Tformat.shortdatepattern = " Yyyy/mm/dd ";d t = convert.todatetime (" 2011/05/26 ", Dtformat);
Method Three: DateTime.ParseExact ()
String datestring = "20110526";D atetime dt = datetime.parseexact (datestring, "YyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Or
DateTime dt = DateTime.ParseExact (datestring, "YyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Reference information:
CultureInfo CultureInfo = cultureinfo.createspecificculture ("en-us"); string format = "ddd MMM d HH:mm:ss zz00 yyyy"; strin G stringvalue = DateTime.Now.ToString (format, cultureInfo); Get date string datetime datetime = DateTime.ParseExact ("Wed 16:28:03 +0800", format, cultureInfo); Convert a string into a date
Other
Date format: YyyyMMdd HH:mm:ss (note the letter case of this string is strict)
YYYY: Year of representation
MM: On behalf of the month
DD: Representative Day
HH: Representative hour (24-hour system)
MM: Represents minutes
SS: Represents seconds
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 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. Dt. Getdatetimeformats (' D ') [2]. ToString ();//week 62,005 to 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, 2005 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, 2005 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 difference in the number of days between 2 dates
DateTime dt1 = Convert.datetime ("2007-8-1"); DateTime DT2 = Convert.datetime ("2007-8-15"); TimeSpan span = dt2. Subtract (DT1); int Daydiff = span. Days + 1;
Calculate the number of days of the month in a year
int days = Datetime.daysinmonth (2007, 8); days = 31;
Add one day to date, one day less
DateTime DT =datetime.now; Dt. AddDays (1); Increase the day dt. AddDays (-1);//decrease one day other years method similar ... Oracle SQL convert Date function to_date ("2007-6-6", ' Yyyy-mm-dd "); To_date ("2007/6/6", "yyyy/mm/dd");