我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34,如果要换成200606,06-2006,2006-6-6或更多的格式该怎么办呢?这里将要用到:DateTime.ToString的方法。
一、DateTime.ToString格式模式
下面列出了DateTime.ToString(string format) 中 format 参数。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
二、应用举例
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007年4月24日
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("D");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007-4-24
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("d");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007年4月24日 16:30:15
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("F");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007年4月24日 16:30
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("f");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007-4-24 16:30:15
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("G");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007-4-24 16:30
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("g");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//16:30:15
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("T");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//16:30
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("t");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007年4月24日 8:30:15
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("U");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007-04-24 16:30:15Z
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("u");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007-04-24 15:52:19
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//2007年04月24 15时56分48秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//星期二, 四月 24 2007
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("dddd, MMMM dd yyyy");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//二, 四月 24 '07
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
this.TextBox2.Text = System.DateTime.Now.ToString("ddd, MMM d \'yy");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//星期二, 四月 24
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("dddd, MMMM dd");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//4-07
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("M/yy");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
//24-04-07
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
System.DateTime.Now.ToString("dd-MM-yy");
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
三、DateTime.Now转换成字符串的其它函数
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
string str = "";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
str += "ToShortDateString:" + DateTime.Now.ToShortDateString() + "<br>";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
str += "ToShortTimeString:" + DateTime.Now.ToShortTimeString() + "<br>";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
str += "ToLongDateString" + DateTime.Now.ToLongDateString() + "<br>";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
str += "ToLongTimeString" + DateTime.Now.ToLongTimeString() + "<br>";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
str += "ToString:" + DateTime.Now.ToString() + "<br>";
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
Response.Write( str );
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
输出结果:
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
ToShortDateString : 2006-2-13
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
ToShortTimeString : 12:51
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
ToLongDateString : 2006年2月13日
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
ToLongTimeString : 12:51:17
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
ToString : 2006-2-13 12:51:17
一、数值类型转换为字符串
C |
货币 |
2.5.ToString("C") |
¥2.50 |
D |
十进制数 |
25.ToString("D5") 25.ToString("D3") |
00025 025 |
E |
科学型 |
25000.ToString("E") |
2.500000E+005 |
F |
固定点 |
25.ToString("F2") 25.ToString("F1") |
25.00 25.0 |
G |
常规 |
2.5.ToString("G") |
2.5 |
N |
数字 |
2500000.ToString("N") 2500000.ToString("N0") |
2,500,000.00 2,500,000 |
X |
十六进制 |
255.ToString("X") |
FF |
###转载于:http://www.studyofnet.com/news/1349.html