C#中的日期处理函数
<wbr><wbr><wbr><wbr> //2007年4月24日<br><wbr><wbr><wbr><wbr> this.TextBox6.Text = System.DateTime.Now.ToString("D");<br><wbr><wbr><wbr><wbr> //2007-4-24<br><wbr><wbr><wbr><wbr> this.TextBox7.Text = System.DateTime.Now.ToString("d");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //2007年4月24日 16:30:15<br><wbr><wbr><wbr><wbr> this.TextBox8.Text = System.DateTime.Now.ToString("F");<br><wbr><wbr><wbr><wbr> //2007年4月24日 16:30<br><wbr><wbr><wbr><wbr> this.TextBox9.Text = System.DateTime.Now.ToString("f");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //2007-4-24 16:30:15<br><wbr><wbr><wbr><wbr> this.TextBox10.Text = System.DateTime.Now.ToString("G");<br><wbr><wbr><wbr><wbr> //2007-4-24 16:30<br><wbr><wbr><wbr><wbr> this.TextBox11.Text = System.DateTime.Now.ToString("g");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //16:30:15<br><wbr><wbr><wbr><wbr> this.TextBox12.Text = System.DateTime.Now.ToString("T");<br><wbr><wbr><wbr><wbr> //16:30<br><wbr><wbr><wbr><wbr> this.TextBox13.Text = System.DateTime.Now.ToString("t");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //2007年4月24日 8:30:15<br><wbr><wbr><wbr><wbr> this.TextBox14.Text = System.DateTime.Now.ToString("U");<br><wbr><wbr><wbr><wbr> //2007-04-24 16:30:15Z<br><wbr><wbr><wbr><wbr> this.TextBox15.Text = System.DateTime.Now.ToString("u");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //4月24日<br><wbr><wbr><wbr><wbr> this.TextBox16.Text = System.DateTime.Now.ToString("m");<br><wbr><wbr><wbr><wbr> this.TextBox16.Text = System.DateTime.Now.ToString("M");<br><wbr><wbr><wbr><wbr> //Tue, 24 Apr 2007 16:30:15 GMT<br><wbr><wbr><wbr><wbr> this.TextBox17.Text = System.DateTime.Now.ToString("r");<br><wbr><wbr><wbr><wbr> this.TextBox17.Text = System.DateTime.Now.ToString("R");<br><wbr><wbr><wbr><wbr> //2007年4月<br><wbr><wbr><wbr><wbr> this.TextBox19.Text = System.DateTime.Now.ToString("y");<br><wbr><wbr><wbr><wbr> this.TextBox19.Text = System.DateTime.Now.ToString("Y");<br><wbr><wbr><wbr><wbr> //2007-04-24T15:52:19.1562500+08:00<br><wbr><wbr><wbr><wbr> this.TextBox20.Text = System.DateTime.Now.ToString("o");<br><wbr><wbr><wbr><wbr> this.TextBox20.Text = System.DateTime.Now.ToString("O");<br><wbr><wbr><wbr><wbr> //2007-04-24T16:30:15<br><wbr><wbr><wbr><wbr> this.TextBox18.Text = System.DateTime.Now.ToString("s");<br><wbr><wbr><wbr><wbr> //2007-04-24 15:52:19<br><wbr><wbr><wbr><wbr> this.TextBox21.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");<br><wbr><wbr><wbr><wbr> //2007年04月24 15时56分48秒<br><wbr><wbr><wbr><wbr> this.TextBox22.Text = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> //星期二, 四月 24 2007<br><wbr><wbr><wbr><wbr> this.TextBox1.Text = System.DateTime.Now.ToString("dddd, MMMM dd yyyy");<br><wbr><wbr><wbr><wbr> //二, 四月 24 '07<br><wbr><wbr><wbr><wbr> this.TextBox2.Text = System.DateTime.Now.ToString("ddd, MMM d \"'\"yy");<br><wbr><wbr><wbr><wbr> //星期二, 四月 24<br><wbr><wbr><wbr><wbr> this.TextBox3.Text = System.DateTime.Now.ToString("dddd, MMMM dd");<br><wbr><wbr><wbr><wbr> //4-07<br><wbr><wbr><wbr><wbr> this.TextBox4.Text = System.DateTime.Now.ToString("M/yy");<br><wbr><wbr><wbr><wbr> //24-04-07<br><wbr><wbr><wbr><wbr> this.TextBox5.Text = System.DateTime.Now.ToString("dd-MM-yy");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
DateTime.Now的后面有若干个ToString(),ToShortTimeString()之类的东东,到底都是干啥用的,还是做个程序测试一下吧
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>str<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>+=<wbr></wbr>"ToShortDateString:"<wbr></wbr>+<wbr>DateTime.Now.ToShortDateString()<wbr></wbr></wbr>+<wbr></wbr>"<br>" ;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>str<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>+=<wbr></wbr>"ToShortTimeString:"<wbr></wbr>+<wbr>DateTime.Now.ToShortTimeString()<wbr></wbr></wbr>+<wbr></wbr>"<br>" ;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>str<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>+=<wbr></wbr>"ToLongDateString"<wbr></wbr>+<wbr>DateTime.Now.ToLongDateString()<wbr></wbr></wbr>+<wbr></wbr>"<br>" ;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>str<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>+=<wbr></wbr>"ToLongTimeString"<wbr></wbr>+<wbr>DateTime.Now.ToLongTimeString()<wbr></wbr></wbr>+<wbr></wbr>"<br>" ;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>str<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>+=<wbr></wbr>"ToString:"<wbr></wbr>+<wbr>DateTime.Now.ToString()<wbr></wbr></wbr>+<wbr></wbr>"<br>" ;
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Response.Write(<wbr>str<wbr>);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
运行结果是:
ToShortTimeString: 12:51
ToLongDateString:2006年2月13日
ToLongTimeString: 12:51:17
ToString: 2006-2-13<wbr></wbr>12:51:17
可以看出,要获得包含年月日时分秒的字符串,只要ToString()就可以
字符型转换转为字符串
<wbr><wbr><wbr> 12345.ToString("n"); //生成 12,345.00<br><wbr><wbr><wbr> 12345.ToString("C"); //生成 ¥12,345.00<br><wbr><wbr><wbr> 12345.ToString("e"); //生成 1.234500e+004<br><wbr><wbr><wbr> 12345.ToString("f4"); //生成 12345.0000<br><wbr><wbr><wbr> 12345.ToString("x"); //生成 3039 (16进制)<br><wbr><wbr><wbr> 12345.ToString("p"); //生成 1,234,500</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>