当前位置: 首页 > article >正文

代码详解C#比较时间的三种方法

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");  
} 

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;  
} 

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(); 
http://www.lryc.cn/news/2418939.html

相关文章:

  • 时间序列 R 09 ARIMA
  • 用php中的ignore_user_abort与register_shutdown_function
  • 神奇的Scheme命名
  • ASP.NET 核心技术 | ADO.NET 使用Connection对象链接数据库
  • 约束优化与医疗科技:提高医疗设备性能的关键技术
  • 精品软件 推荐 常用软件 游戏的 运行库 下载 合集 3D game 玩游戏的一定要收藏一下。...
  • 009 - VulnHub靶机:pWnOS1.0学习笔记——文件包含+密码破解+Perl Cgi反弹shell + linux内核漏洞提权+shellshock漏洞提权
  • InvalidateRect是干啥的
  • 批量doc转docx方法,使用软件、插件
  • 小世界现象
  • 15、wpf之button样式小记
  • 数据开发流程及规范
  • 全球时区 简称 缩写 简介 PST EST GMT CST EDT UTC 等
  • 熊猫烧香病毒分析报告
  • CodeIgniter框架深入理解
  • 什么是基站定位?基站定位的原理及特点
  • Zblog主题模板:zblog博客网站建设_博客文章网站源码免费下载
  • 集合线程安全控制-Collections工具类synchronizedXxx()方法
  • 飞客蠕虫(Conficker)
  • Python爬虫实战:爬取拉勾网并对其进行数据分析
  • 八段数码管动态显示(汇编+C语言)
  • FastDB 很难得的一篇分析
  • 使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)
  • 2款安卓漫画阅读器横评:漫画阅读软件哪个好用
  • 世界各国与中国时差查询
  • Sailfish OS
  • 离散数据编码方式总结(OneHotEncoder、LabelEncoder、OrdinalEncoder、get_dummies、DictVectorizer、to_categorical的区别?)
  • java.lang.Integer
  • linux进程优先级与nice,Linux之renice和nice调整进程的优先级
  • ActivityThread源码分析