aspx中格式化时间的几种方式
2007-10-26 10:03
昨天在修改以前的.net程序时,发现了aspx中的几种格式化时间的表示方法总结如下(测试环境vs2005+sql2005:
DataList中: <%# DataBinder.Eval(Container.DataItem,"join_date","{0:D}") %>
出来的时间格式是: 2007年10月25日
<%# DataBinder.Eval(Container.DataItem,"join_date","{0:yyyy-MM-dd}") %>
出来的时间格式是: 2007-10-25
显示英文时间
前台代码:<%# FormatDate(DataBinder.Eval(Container.DataItem,"join_date")) %>
函数: public string FormatDate(object date)
{
System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo("en-US", false).DateTimeFormat;
DateTime dt = Convert.ToDateTime(date);
return dt.ToString("D", myDTFI);
}
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
DataGrid中:<asp:BoundColumn DataField="join_date" HeaderText="加 入 日 期" DataFormatString="{0:yyyy-MM-dd}"><HeaderStyle Width="22%" /></asp:BoundColumn>
出来的时间格式为:2007-08-11