在GridView中格式化日期
时间:2010-08-23 来源:08信工实训
一、使用模板列
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("datetime", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
这样,在列中,它显示:2007-11-6
二、进行简单的设置
如果要应用 formatstring ,必须将该列 Behavior-> HtmlEncode 设置为False
这样就可以格式化成功了。htmlEncode主要是防止代码攻击
==================================================================
格式名 说明
General Date,或者 G
显示日期和/或时间。例如 4/3/93 05:34 PM。日期显示由系统的 LocaleID 值确定。
Long Date、Medium Date 或 D
根据区域设置的长日期格式显示日期。
Short Date,或者 d
使用区域设置的短日期格式显示日期。
Long Time、Medium Time 或 T
使用区域设置的长时间格式显示时间,通常包括小时、分钟、秒。
Short Time,或者 t
使用区域设置的短时间格式显示时间。
f
根据您的区域设置格式显示长日期和短时间。
F
根据您的区域设置格式显示长日期和长时间。
g
根据您的区域设置格式显示短日期和短时间。
M, m
显示日期的月份和星期几。
R, r
将日期和时间格式化为格林威治标准时间 (GMT)。
s
将日期和时间格式化为可排序的索引。
u
将日期和时间格式化为 GMT 可排序索引。
U
用长日期和长时间将日期和时间格式化为 GMT。
Y, y
将日期格式化为年和月份