DataFormatString="{0:yyyy-MM-dd 17:00:00}"
Convert.ToDateTime("2008-05-06" + " 23:59:59")
C#日期格式化1、綁定時(shí)格式化日期方法:
<ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " >
ITEMSTYLE WIDTH= "18% " > </ITEMSTYLE >
</ASP:BOUNDCOLUMN >
C#日期格式化2、數據控件如DataGrid/DataList等的件格式化日期方法:
e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();
C#日期格式化3、用String類(lèi)轉換日期顯示格式:
String.Format( "yyyy-MM-dd ",yourDateTime);
C#日期格式化4、用Convert方法轉換日期顯示格式:
yyyy-MM-dd HH:mm:ss 24小時(shí)制 yyyy-MM-dd hh:mm:ss 12小時(shí)制
Convert.ToDateTime("2005-8-23").ToString("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo); //支持繁體數據庫
C#日期格式化5、直接用ToString方法轉換日期顯示格式:
DateTime.Now.ToString("yyyyMMddhhmmss");
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
C#日期格式化6、只顯示年月
DateTime.Now.ToString("yyyyMMddhhmmss");
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
C#日期格式化7、顯示時(shí)間所有部分,包括:年月日時(shí)分秒
<asp:BoundColumn DataField="收款時(shí)間" HeaderText="收款時(shí)間"
DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}">
</asp:BoundColumn>
C#日期格式化8、隱藏代碼:
protected string CutTime (object time)
{
System.DateTime currentTime = new System.DateTime();
return Convert.ToDateTime(time).ToString("yy-MM-dd",System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
下面是html代碼:
1. <td width="10%" align="center" valign="middle"><%# CutTime(DataBinder.Eval(Container, "DataItem.addtime")) %></td>
二:C#比較時(shí)間的三種方法
比較時(shí)間大小
stringstr1="12:12";
stringstr2="14:14";
DateTimedt1=Convert.ToDateTime(str1);
DateTimedt2=Convert.ToDateTime(str2);
DateTimedt3=DateTime.Now;
if(DateTime.Compare(dt1,dt2)>0)//大于
{
Response.Write("str1>str2");
}
elseif(DateTime.Compare(dt1,dt2)<0)//小于
{
Response.Write("str1<str2");
}
elseif(DateTime.Compare(dt1,dt2)==0)//相等
{
Response.Write("str1==str2");
}
stringstr1="12:12";
stringstr2="14:14";
DateTimedt1=Convert.ToDateTime(str1);
DateTimedt2=Convert.ToDateTime(str2);
DateTimedt3=DateTime.Now;
if(DateTime.Compare(dt1,dt2)>0)//大于
{
Response.Write("str1>str2");
}
elseif(DateTime.Compare(dt1,dt2)<0)//小于
{
Response.Write("str1<str2");
}
elseif(DateTime.Compare(dt1,dt2)==0)//相等
{
Response.Write("str1==str2");
}
計算兩個(gè)時(shí)間差值的函數,返回時(shí)間差的絕對值:
privatestringDateDiff(DateTimeDateTime1,DateTimeDateTime2)
{
string dateDiff=null;
try
{
TimeSpan ts1=newTimeSpan(DateTime1.Ticks);
TimeSpan ts2=newTimeSpan(DateTime2.Ticks);
TimeSpan ts=ts1.Subtract(ts2).Duration();
dateDiff=ts.Days.ToString()+"天"
+ts.Hours.ToString()+"小時(shí)"
+ts.Minutes.ToString()+"分鐘"
+ts.Seconds.ToString()+"秒";
}
catch
{
}
returndateDiff;
}
privatestringDateDiff(DateTimeDateTime1,DateTimeDateTime2)
{
stringdateDiff=null;
try
{
TimeSpants1=newTimeSpan(DateTime1.Ticks);
TimeSpants2=newTimeSpan(DateTime2.Ticks);
TimeSpants=ts1.Subtract(ts2).Duration();
dateDiff=ts.Days.ToString()+"天"
+ts.Hours.ToString()+"小時(shí)"
+ts.Minutes.ToString()+"分鐘"
+ts.Seconds.ToString()+"秒";
}
catch
{
}
returndateDiff;
}
實(shí)現計算DateTime1-40天=DateTime2的功能
TimeSpants=newTimeSpan(40,0,0,0);
DateTimedt2=DateTime.Now.Subtract(ts);
msg.Text=DateTime.Now.ToString()+"-"+ts.Days.ToString()+"天\r\n";
msg.Text+=dt2.ToString();
計算時(shí)間差:
TimeSpan ts5 = new TimeSpan(DateTime.Now.Ticks);
code....
TimeSpan ts6 = new TimeSpan(DateTime.Now.Ticks);
string time = ts6.Subtract(ts5).TotalSeconds.ToString();
Core.SmartMessageBox.AjaxShow(this, time);
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。