One. Date Format
1.ToString()
d month one day. Single-digit days without leading zeros. dd month a day. Single-digit days have a leading zero. ddd day of the week abbreviated name, defined in the AbbreviatedDayNames. dddd full name of the day of the week, as defined in DayNames in. M month number. Digit months without leading zeros. MM month number. Single-digit months have a leading zero. MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames in. MMMM The full name of the month, as defined in MonthNames in. y Year without the century. If the year without the century is less than 10 , the display does not have a leading zero year. yy Year without the century. If the year without the century is less than 10 , a leading zero year is displayed. yyyy era including the four-digit year. gg period or era. To set the date format that does not have an associated period or era string is ignored in this mode. H 12 is hour to hour. Single-digit hours without leading zeros. HH 12 is hour-hour clock. Single-digit hours have a leading zero. H 24 -hour-hour clock. Single-digit hours without leading zeros. HH 24 -hour clock. Single-digit hours have a leading zero. m min. Single-digit minutes without leading zeros. mm minutes. Single-digit minutes have a leading zero. s seconds. Single-digit seconds without leading zeros. ss seconds. Single-digit seconds have a leading zero. decimal precision f s is a. The remaining digits are truncated. ff decimal precision for two seconds. The remaining digits are truncated. fff decimal precision for three seconds. The remaining digits are truncated. ffff decimal precision for four seconds. The remaining digits are truncated. fffff decimal precision for five seconds. The remaining digits are truncated. ffffff decimal precision of six seconds. The remaining digits are truncated. fffffff decimal precision for seven seconds. The remaining digits are truncated. t PMDesignator defined in AMDesignator or the AM / the PM item indicating the first character (if present). tt PMDesignator defined in AMDesignator or the AM / the PM designator (if present). z time zone offset ( " +" or "-" followed by the hour only). Single-digit hours without leading zeros. For example, Pacific Standard Time is "- 8 ." zz time zone offset ( " +" or "-" followed by the hour only). Single-digit hours have a leading zero. For example, Pacific Standard Time is "- 08 ." zzz The full time zone offset ( " +" or "-" followed by hours and minutes). Single-digit hours and minutes with leading zeros. For example, Pacific Standard Time is "-08 : 00 . " : Delimiter default time in TimeSeparator defined / default date defined in DateSeparator delimiters. % C where c is the format pattern (if used alone) if the format mode and literal characters or other format. the combined mode, it is possible to omit the "% " character. c wherein c is any character. according displays the character. to display the backslash character, use the "\". only listed above second table format mode It can be used to create custom mode; standard format characters listed in the first table can not be used to create custom mode custom mode length at least two characters; for example, DateTime.ToString ( " D " ) returns DateTime value; "d" is the standard short date mode. DateTime.ToString ( " % D " ) returns the day of the month; "% D" is a custom mode. DateTime.ToString ( " D " ) followed by a return to One day in mid-blank character; "d" is a custom mode as To get such a format in June 2005, the time can be written: More convenient that the above parameters can be freely combined, and can not go wrong, try more, and will definitely find the time format you want Date.toString ( " yyyy Year MM month " , DateTimeFormatInfo.InvariantInfo)
2.
DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2.Text = dt.ToFileTime().ToString();//127756416859912816 Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816 Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25 Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日 Label6.Text = dt.ToLongTimeString().ToString();//13:21:25 Label7.Text = dt.ToOADate().ToString();//38661.5565508218 Label8.Text = dt.ToShortDateString().ToString();//2005-11-5 Label9.Text = dt.ToShortTimeString().ToString();//13:21 Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25 Label1.Text = dt.Year.ToString();//2005 Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00 Label3.Text = dt.DayOfWeek.ToString();//Saturday Label4.Text = dt.DayOfYear.ToString();//309 Label5.Text = dt.Hour.ToString();//13 Label6.Text = dt.Millisecond.ToString();//441 Label7.Text = dt.Minute.ToString();//30 Label8.Text = dt.Month.ToString();//11 Label9.Text = dt.Second.ToString();//28 Label10.Text = dt.Ticks.ToString();//632667942284412864 Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864 Label1.Text = dt.ToString();//2005-11-5 13:47:04 Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04 Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04 Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04 Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04 Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04 Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05 Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10 Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04 Label10.Text = dt.CompareTo(dt).ToString();//0 Label11.Text = dt.Add(?).ToString();//问号为一个时间段 Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False Label2.Text = dt.Equals(dt).ToString();//True Label3.Text = dt.GetHashCode().ToString();//1474088234 Label4.Text = dt.GetType().ToString();//System.DateTime Label5.Text = dt.GetTypeCode().ToString();//DateTime Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25 Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06 = Dt.GetDateTimeFormats Label3.Text ( ' Y ' ) [ 0 ] .ToString (); // November 2005 Label4.Text = dt.GetDateTimeFormats ( ' D ' ) [ 0 ] .ToString (); // 2005 5 November Label5.Text = dt.GetDateTimeFormats ( ' D ' ) [ . 1 ] .ToString (); // 2005 05. 11 Label6.Text = dt.GetDateTimeFormats ( ' D ' ) [ 2 ] .ToString (); / / Sat. 11 05 2005 Label7.Text = dt.GetDateTimeFormats ( ' D ' ) [. 3 ] .ToString (); // Saturday, November 5, 2005 Label8.Text = dt.GetDateTimeFormats ( ' M ' ) [ 0 ] .ToString (); // 5 November Label9.Text = dt.GetDateTimeFormats ( ' F ' ) [ 0 ] .ToString (); // November 2005. 5 Day 14:06 Label10.Text = dt.GetDateTimeFormats ( ' G ' ) [ 0 ] .ToString (); // 2005-11-5 14:06 Label11.Text = dt.GetDateTimeFormats ( ' R & lt ' ) [ 0 ] .ToString (); // Sat, 05-Nov 2005 14:06:25 GMT Label1.Text = string.Format("{0:d}",dt);//2005-11-5 Label2.Text = string.Format("{0:D}",dt);//2005年11月5日 Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23 Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23 Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23 Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23 Label7.Text = string.Format("{0:M}",dt);//11月5日 Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23 Label10.Text string.Format("{0:t}",dt);//14:23 Label11.Text = string.Format("{0:T}",dt);//14:23:23 Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23 Label14.Text = string.Format("{0:Y}",dt);//2005年11月 Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23 Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);
two. Time for size
//比较时间大小的实验 string st1="12:13"; string st2="14:14"; DateTime dt1=Convert.ToDateTime(st1); DateTime dt2=Convert.ToDateTime(st2); DateTime dt3=DateTime.Now; if(DateTime.Compare(dt1,dt2)>0) msg.Text=st1+">"+st2; else msg.Text=st1+"<"+st2; msg.Text+=" \ R & lt \ n- " + dt1.ToString (); IF (DateTime.Compare (DT1, DT3)> 0 ) msg.Text + = " \ R & lt \ n- " + + ST1 " > " + dt3.ToString (); the else msg.Text + = " \ R & lt \ n- " + ST1 + " < " + dt3.ToString (); // calculate the time difference between the two function returns the absolute value of the time difference: Private String the DateDiff (the DateTime DateTime1, the DateTime DateTime2) { String DateDiff = null ; the try { TimeSpan ts1=new TimeSpan(DateTime1.Ticks); TimeSpan ts2=new TimeSpan(DateTime2.Ticks); TimeSpan ts=ts1.Subtract(ts2).Duration(); dateDiff=ts.Days.ToString()+"天" +ts.Hours.ToString()+"小时" +ts.Minutes.ToString()+"分钟" +ts.Seconds.ToString()+"秒"; } catch { } return dateDiff; } //实现计算DateTime1-36天=DateTime2的功能 TimeSpan ts=new TimeSpan(40,0,0,0); DateTime dt2=DateTime.Now.Subtract(ts); msg.Text=DateTime.Now.ToString()+"-"+ts.Days.ToString()+"天\r\n"; msg.Text+=dt2.ToString();
three. Date format commonly used method
Add TimeSpan value of the specified value of the applied example of the
number of hours specified AddHours instance on the value added
AddMilliseconds the specified number of milliseconds from the value applied to instances
AddMinutes the specified number of minutes is added to the value instances
AddMonths examples of the value to the specified number of months
to the value of the specified number of seconds AddSeconds applied example of
the year AddYears applied to the specified instance of the value
four. Date of acquisition of the week
String [] Day = new new String [] { " Sunday " , " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " , " Saturday " }; String Week Day = [Convert.ToInt32 (DateTime .Now.DayOfWeek.ToString ( " D " .))] the ToString (); // acquired week number corresponding to the date the DateTime Time = the DateTime.Now; int week = ( int ) Time.DayOfWeek;