如何计算两个日期之间的日差 [英] How to calculate day difference between two dates

查看:63
本文介绍了如何计算两个日期之间的日差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii朋友......



如何计算两个日期之间的总天数差异.. ??



提前谢谢。

解决方案

假设您有Date1和Date2,它们都是DateTime类型,您希望计算它们在整天的差异。



使用TimeSpan类计算两个日期之间的差异。

 TimeSpan diff = myDate2。减去(myDate1); 





然后将结果(差异)转换为整天。

  double  DaysDiff = diff.TotalDays; 


如果要在sql查询中执行此操作,请使用 DATEDIFF [ ^ ] function。


in sql

选择DATEDIFF(d,''1 / 10/2012'',''1/20/2012'')找到天数

格式MM / dd / yyyy in sql

in c#

date1

date2

timespan st = date1.subtract(date2);

string totaldate = st.totaldays.tostring();

Hii friends...

How to calculate total days diffrence between two dates..??

Thanks in advance.

解决方案

Lets assume you have Date1 and Date2 which are both DateTime type and you wish to calculate the difference between them in entire days.

You calculate the difference between the two dates, using the TimeSpan class.

TimeSpan diff = myDate2.Subtract(myDate1);  



Then you convert the result ("diff") to whole days.

double DaysDiff = diff.TotalDays;  


If you want to do it in sql query, use DATEDIFF[^] function.


in sql
select DATEDIFF(d,''1/10/2012'',''1/20/2012'') find days
format MM/dd/yyyy in sql
in c#
date1
date2
timespan st=date1.subtract(date2);
string totaldate=st.totaldays.tostring();


这篇关于如何计算两个日期之间的日差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆