主要使用ToLongDateString、ToShortDateString、ToLongTimeString、ToShortTimeString、DayOfWeek等获取日期、时间、星期等数据。
MM =两位数月份
mm =两位数分钟
HH =两位小时,24小时制
hh =两位小时,12小时制
string br = "
"; string a1 = DateTime.Now.ToString(); string a2 = DateTime.Now.ToString("yyyy-MM-dd"); string a3 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string a4 = DateTime.Now.ToString("yyyyMMddHHmmss"); string a5 = DateTime.Now.ToLongDateString().ToString(); string a6 = DateTime.Now.ToString("hh:mm:ss"); string a7 = DateTime.Now.Year.ToString(); string a8 = DateTime.Now.DayOfWeek.ToString(); Response.Write(a1 + br); Response.Write(a2 + br); Response.Write(a3 + br); Response.Write(a4 + br); Response.Write(a5 + br); Response.Write(a6 + br); Response.Write(a7 + br); Response.Write(a8 + br);
显示的效果如下图: