对于时间的显示问题asp.net中获取年月日时间
//获取日期+时间
DateTime.Now.ToString(); // 2008-9-4 20:02:10
DateTime.Now.ToLocalTime().ToString(); // 2008-9-4 20:12:12
//获取日期
DateTime.Now.ToLongDateString().ToString(); // 2008年9月4日
DateTime.Now.ToShortDateString().ToString(); // 2008-9-4
DateTime.Now.ToString("yyyy-MM-dd"); // 2008-09-04
DateTime.Now.Date.ToString(); // 2008-9-4 0:00:00
//获取时间
DateTime.Now.ToLongTimeString().ToString(); // 20:16:16
DateTime.Now.ToShortTimeString().ToString(); // 20:16
DateTime.Now.ToString("hh:mm:ss"); // 08:05:57
DateTime.Now.TimeOfDay.ToString(); // 20:33:50.7187500
//其他DateTime.ToFileTime().ToString(); // 128650040212500000
DateTime.Now.ToFileTimeUtc().ToString(); // 128650040772968750
DateTime.Now.ToOADate().ToString(); // 39695.8461709606
DateTime.Now.ToUniversalTime().ToString(); // 2008-9-4 12:19:14
DateTime.Now.Year.ToString(); 获取年份 // 2008
DateTime.Now.Month.ToString(); 获取月份 // 9
DateTime.Now.DayOfWeek.ToString(); 获取星期 // Thursday
DateTime.Now.DayOfYear.ToString(); 获取第几天 // 248
DateTime.Now.Hour.ToString(); 获取小时 // 20
DateTime.Now.Minute.ToString(); 获取分钟 // 31
DateTime.Now.Second.ToString(); 获取秒数 // 45
//n为一个数,可以数整数,也可以事小数
dt.AddYears(n).ToString(); //时间加n年
dt.AddDays(n).ToString(); //加n天
dt.AddHours(n).ToString(); //加n小时
dt.AddMonths(n).ToString(); //加n个月
dt.AddSeconds(n).ToString(); //加n秒
dt.AddMinutes(n).ToString(); //加n分
------------------------------------------------------------------------------------------------------------------------
2008-09-19 12:36this.ltlDate.Text=DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月"+DateTime.Now.Day.ToString()+"日 "+this.GetWeekDay();
private string GetWeekDay()
{
string Temp="";
switch (DateTime.Now.DayOfWeek)
{
case DayOfWeek.Sunday:
Temp = "星期天";
break;
case DayOfWeek.Monday:
Temp = "星期一";
break;
case DayOfWeek.Tuesday:
Temp = "星期二";
break;
case DayOfWeek.Wednesday:
Temp = "星期三";
break;
case DayOfWeek.Thursday:
Temp = "星期四";
break;
case DayOfWeek.Friday:
Temp = "星期五";
break;
case DayOfWeek.Saturday:
Temp = "星期六";
break;
}
return Temp;
}
其它:
1.绑定时格式化日期方法:
<ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " >
<ITEMSTYLE WIDTH= "18% " > </ITEMSTYLE >
</ASP:BOUNDCOLUMN > 2.数据控件如DataGrid/DataList等的件格式化日期方法:
e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();
3.用String类转换日期显示格式:
String.Format( "yyyy-MM-dd ",yourDateTime);
4.用Convert方法转换日期显示格式:
Convert.ToDateTime("2005-8-23").ToString
("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo); //支持繁体数据库
5.直接用ToString方法转换日期显示格式:
DateTime.Now.ToString("yyyyMMddhhmmss");
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
6.只显示年月
DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}")
7.显示时间所有部分,包括:年月日时分秒
<asp:BoundColumn DataField="收款时间" HeaderText="收款时间"
DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}">
</asp:BoundColumn>