Datatime nowdate = Datatime.now; Nowdate.tostring ("R"); Effect: Wed, June June 2009 16:24:33 GMT Search the entire network, it is so .... Depressed in ... Find all the time formatting for this D ShortDatePattern D Longdatepattern F Full Date and time (long date and short time) F Fulldatetimepattern (long date and long time) G General (short date and short time) G General (short date and long time) M, M Monthdaypattern R, R Rfc1123pattern s use local time Sortabledatetimepattern (based on ISO 8601) T Shorttimepattern T Longtimepattern U universalsortabledatetimepattern format for displaying Universal Time U use the full date and time of the universal Time (long date and long time) Y, y Yearmonthpattern The following table lists the patterns that can be merged to construct a custom pattern ======================================== These patterns are case-sensitive; for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space characters or characters enclosed in single quotes, the output string page will also contain those characters. Characters that are not defined as part of a format pattern or that are not defined as format characters are copied verbatim. Format Mode Description: D One day of the month. The date of one number does not have a leading zero. One day of DD month. The date of a single number has a leading zero. The abbreviated name of a day in the DDD week, defined in Abbreviateddaynames. dddd the full name of the day of the week, defined in DayNames. M-month numbers. A single number of months does not have a leading zero. MM month number. A one-digit month has a leading zero. The abbreviated name of the MMM month, defined in AbbreviatedMonthNames. The full name of the MMMM month, defined in MonthNames. Y does not contain the year of the era. If the year that does not contain the era is less than 10, the year is displayed with no leading zeros. YY does not contain years of the era. If the year that does not contain the era is less than 10, the year with leading zeros is displayed. YYYY includes the four-digit year of the era. GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored. H 12 Hour system. The number of hours in a single digit does not have a leading zero. HH 12-hour hour. The number of hours in a single digit has a leading zero. H 24 hour system. The number of hours in a single digit does not have a leading zero. HH 24-hour hour. The number of hours in a single digit has a leading zero. M minutes. The number of minutes in one digit does not have a leading zero. MM minutes. The number of minutes in a single digit has a leading zero. s seconds. A number of seconds does not have a leading zero. SS seconds. The number of seconds in a single digit has a leading zero. The fractional precision of f seconds is one digit. The remaining digits are truncated. The fractional precision of FF seconds is two digits. The remaining digits are truncated. The decimal precision for fff seconds is three digits. The remaining digits are truncated. The decimal precision for ffff seconds is four digits. The remaining digits are truncated. The decimal precision for fffff seconds is five digits. The remaining digits are truncated. The decimal precision for ffffff seconds is six digits. The remaining digits are truncated. The decimal precision for fffffff seconds is seven digits. The remaining digits are truncated. t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists. The AM/PM indication (if any) that TT is defined in AMDesignator or PMDesignator. Z Time zone offset ("+" or "-" followed only by hours). The number of hours in a single digit does not have a leading zero. For example, the Pacific Standard Time is "-8". The ZZ Time zone offset ("+" or "-" follows only the hour). The number of hours in a single digit has a leading zero. For example, the Pacific Standard Time is "-08". ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes for a single digit has a leading zero. For example, the Pacific Standard Time is " -08:00". : The default time separator defined in TimeSeparator. /the default date separator defined in DateSeparator. % c where C is the format pattern (if used alone). If the format pattern is merged with a literal character or another format pattern, you can omit the "%" character. \ c where c is any character. Display characters according to the literal meaning. To display the backslash character, use "\". Only the format patterns listed in the second table above can be used to create custom patterns; The standard format characters listed in the first table cannot be used to create custom patterns. The length of the custom pattern is at least two characters; DateTime.ToString ("D") returns the DateTime value, and "D" is the standard short date pattern. DateTime.ToString ("%d") returns the day of the month, and "%d" is a custom pattern. DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern. It is convenient that the above parameters can be arbitrarily combined, and do not go wrong, try more, you will find the time format To get the time of June 2005 in such a format You can write this: Date. ToString ("YYYY year mm month", Datetimeformatinfo.invariantinfo) So the analogy. The following is a list of specific date formatting usages in some asp.net: ============================================ 1. Format Date method of binding timing: 2. Formatting date methods for data controls such as Datagrid/datalist: E.item.cell[0]. Text = Convert.todatetime (e.item.cell[0]. Text). ToShortDateString (); 3. Convert date display format with string class: String.Format ("Yyyy-mm-dd", yourdatetime); 4. Convert date display format using the Convert method: Convert.todatetime ("2005-8-23"). Tostring ("YyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); Support for traditional databases 5. Convert date display format directly with the ToString method: DateTime.Now.ToString ("Yyyymmddhhmmss"); DateTime.Now.ToString ("Yyyy/mm/dd hh:mm:ss") 6. Show only years DataBinder.Eval (Container.DataItem, "StartTime", "{0:yyyy-m}") 7. Show all parts of time, including: Date and hour Dataformatstring= ' {0:yyyy-mm-dd HH24:mm:ss} ' > Use DateTime.ToString (string format) to output dates in different formats The DateTime.ToString () function has four overloads. The most commonly used is the one with no parameters. DateTime.ToString (string format) is more powerful and can output dates in different formats. Some of the following are listed for your reference. Some of them are not listed on MSDN. 1. Y represents the year, the note is lowercase y, and the uppercase Y does not represent the year. 2. m represents the month. 3. D indicates the date, noting that D does not represent anything. 4. h or H for hours, h for 12-hour system, h for 24-hour system. 5. m represents minutes. 6. s represents seconds. Note that s doesn't mean anything. Format Output Example Years Y 7 String yy = DateTime.Now.ToString ("y-mm") Yy= "7-05" Yy 07 String yy = DateTime.Now.ToString ("yy-mm") Yy= "07-05" yyy or more Y 1984 String yy = DateTime.Now.ToString ("yyyy"); Yy= "2007" Month M 5. String mon = DateTime.Parse ("1984-05-09") ToString ("Yyyy-m") Mon = "1984-5" Mm 05. String mon = DateTime.Parse ("1984-05-09") ToString ("MM") Mon = "05" MMM If it is a Chinese version of the operating system, it will be output: May. If it is an English operating system, enter the first three-letter shorthand for the month: May String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM") English version of the operating system: April Chinese version of the operating system: July Mmmm or more M If it is a Chinese version of the operating system, it will be output: May. If it is an English operating system, enter full write for the month String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM") English version of the operating system: July Chinese version of the operating system: July Date or week D 9 String dd= datetime.parse ("1984-05-09") ToString ("D") Dd= "9" Dd 09 String dd= datetime.parse ("1984-05-09") ToString ("DD") dd= "09" Ddd If it is a Chinese version of the operating system, it will output weeks, such as Wednesday ... In the case of an English operating system, the abbreviation for the week is output: Wed string dd = DateTime.Parse ("2006-07-01"). ToString ("ddd") English version of the operating system: Wed Chinese version of the operating system: Wednesday dddd or more D If it is a Chinese version of the operating system, it will output weeks, such as Wednesday ... If it is an English operating system, the output week: Wednesday string dd = DateTime.Parse ("2006-07-01"). ToString ("dddd") English version of the operating system: Wednesday Chinese version of the operating system: Wednesday Hours H Hour Range: 1-12 string hh = DateTime.Now.ToString ("h"); HH = 8 HH or more H Hour Range: 1-12 string hh = DateTime.Now.ToString ("hh"); HH = 08 H Hour Range: 0-23 string hh = DateTime.Now.ToString ("yyyy-h"); HH = 2006-8 HH or more H Hour Range: 0-23 string hh = DateTime.Now.ToString ("yyyy-hh"); HH = 2006-08 string hh = Datetime.pare ("2006-7-4 18:00:00"). ToString ("yyyy-hh"); HH = 2006-18 Minutes M 6 string mm = DateTime.Now.ToString ("yyyy-mm-dd-m"); MM = "2006-07-01-6"; mm or more M 06 string mm = DateTime.Now.ToString ("yyyy-mm-dd-mm"); MM = "2006-07-01-06"; Seconds S 6 string mm = DateTime.Now.ToString ("Yyyy-mm-dd-s"); MM = "2006-07-01-6"; SS or more S 06 string mm = DateTime.Now.ToString ("Yyyy-mm-dd-ss"); MM = "2006-07-01-06"; |