format in datetime
来源:互联网 发布:英国女王 知乎 编辑:程序博客网 时间:2024/04/30 14:48
using System;
using System.Globalization;
public class MainClass {
public static void Main(string[] args) {
DateTime dt = DateTime.Now;
String[] format = {
"d", "D",
"f", "F",
"g", "G",
"m",
"r",
"s",
"t", "T",
"u", "U",
"y",
"dddd, MMMM dd yyyy",
"ddd, MMM d /"'/"yy",
"dddd, MMMM dd",
"M/yy",
"dd-MM-yy",
};
String date;
for (int i = 0; i < format.Length; i++) {
date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
Console.WriteLine(String.Concat(format[i], " :" , date));
}
/** Output.
*
* d :08/17/2000
* D :Thursday, August 17, 2000
* f :Thursday, August 17, 2000 16:32
* F :Thursday, August 17, 2000 16:32:32
* g :08/17/2000 16:32
* G :08/17/2000 16:32:32
* m :August 17
* r :Thu, 17 Aug 2000 23:32:32 GMT
* s :2000-08-17T16:32:32
* t :16:32
* T :16:32:32
* u :2000-08-17 23:32:32Z
* U :Thursday, August 17, 2000 23:32:32
* y :August, 2000
* dddd, MMMM dd yyyy :Thursday, August 17 2000
* ddd, MMM d "'"yy :Thu, Aug 17 '00
* dddd, MMMM dd :Thursday, August 17
* M/yy :8/00
* dd-MM-yy :17-08-00
*/
}
}
Format Character | Associated Property/ Description |
---|---|
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 | SortableDateTimePattern (based on ISO 8601) using local time |
t | ShortTimePattern |
T | LongTimePattern |
u | UniversalSortableDateTimePattern using the format for universal time display |
U | Full date and time (long date and long time) using universal time |
y, Y | YearMonthPattern |
The following table lists the patterns that can be combined to construct custom patterns. The patterns are case-sensitive; for example, "MM" is recognized, but "mm" is not. If the custom pattern contains white-space characters or characters enclosed in single quotation marks, the output string will also contain those characters. Characters not defined as part of a format pattern or as format characters are reproduced literally.
Format Pattern | Description |
---|---|
d | The day of the month. Single-digit days will not have a leading zero. |
dd | The day of the month. Single-digit days will have a leading zero. |
ddd | The abbreviated name of the day of the week, as defined in AbbreviatedDayNames. |
dddd | The full name of the day of the week, as defined in DayNames. |
M | The numeric month. Single-digit months will not have a leading zero. |
MM | The numeric month. Single-digit months will have a leading zero. |
MMM | The abbreviated name of the month, as defined in AbbreviatedMonthNames. |
MMMM | The full name of the month, as defined in MonthNames. |
y | The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. |
yy | The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero. |
yyyy | The year in four digits, including the century. |
gg | The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string. |
h | The hour in a 12-hour clock. Single-digit hours will not have a leading zero. |
hh | The hour in a 12-hour clock. Single-digit hours will have a leading zero. |
H | The hour in a 24-hour clock. Single-digit hours will not have a leading zero. |
HH | The hour in a 24-hour clock. Single-digit hours will have a leading zero. |
m | The minute. Single-digit minutes will not have a leading zero. |
mm | The minute. Single-digit minutes will have a leading zero. |
s | The second. Single-digit seconds will not have a leading zero. |
ss | The second. Single-digit seconds will have a leading zero. |
f | The fraction of a second in single-digit precision. The remaining digits are truncated. |
ff | The fraction of a second in double-digit precision. The remaining digits are truncated. |
fff | The fraction of a second in three-digit precision. The remaining digits are truncated. |
ffff | The fraction of a second in four-digit precision. The remaining digits are truncated. |
fffff | The fraction of a second in five-digit precision. The remaining digits are truncated. |
ffffff | The fraction of a second in six-digit precision. The remaining digits are truncated. |
fffffff | The fraction of a second in seven-digit precision. The remaining digits are truncated. |
t | The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. |
tt | The AM/PM designator defined in AMDesignator or PMDesignator, if any. |
z | The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8". |
zz | The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08". |
zzz | The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00". |
: | The default time separator defined in TimeSeparator. |
/ | The default date separator defined in DateSeparator. |
% c | Where c is a format pattern if used alone. The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns. |
/ c | Where c is any character. Displays the character literally. To display the backslash character, use "//". |
- format in datetime
- Datetime Format Elements in oracle
- How to format DateTime in PowerShell window
- How to format date/datetime in struts2
- Format DateTime
- Datetime format
- C# datetime format
- DateTime format log
- DateTime的Format格式
- DateTime Format String
- datetime string format
- string.Format DateTime
- Tcl/TK -- DateTime Format
- T-SQL DateTime Format
- C# 时间格式 DateTime Format
- C# 时间格式 DateTime Format
- C# 时间格式 DateTime Format
- String Format for DateTime [C#]
- 使用硬连接Oracle数据库
- 使用Object Pascal中的接口访问Visual C++ DLL中的对象
- native2ascii-本地码-至-ASCII 码转换器
- ARPU
- 使用Acctinfo.dll了解更多AD用户帐号信息
- format in datetime
- 唐僧情史[转载]
- Eclipse+Jboss使用小问题总结
- 使用dom4j时出现的一个与目录相关的错误,及解决方法
- SQL Server端口更改后的数据库连接方式
- sar在维护UNIX中的使用讲解
- 使用WMI来获取CPU序列号
- 风儿轻轻,云也飘飘 2004年11月10日, 17:22:51
- 互联网接入另类方案:内网建站新思路