C#比较时间方法1
比较时间大小的实验
C#代码
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");
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
C#比较时间方法2
计算两个时间差值的函数,返回时间差的绝对值:
C#代码
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()+"小时"
+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()+"小时"
+ts.Minutes.ToString()+"分钟"
+ts.Seconds.ToString()+"秒";
}
catch
{
}
returndateDiff;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
C#比较时间方法3
实现计算DateTime1-40天=DateTime2的功能
C#代码
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();
- 1.
- 2.
- 3.
- 4.
【编辑推荐】