如何计算使用日期时间选择器之间的天数 [英] how to count days between using datetime picker

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

问题描述

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{  
   DateTime start = dateTimePicker1.Value.Date;
   DateTime end = dateTimePicker2.Value.Date;
   txttotal_days.Text=Convert.ToInt32(end.Subtract(start).Days).ToString();
}


我使用上面的编码来查找使用datetimepicker的日期,[当我尝试在按钮内部正常工作时]


I used above coding to find no of days using datetimepicker,[when I try inside button its working fine]

推荐答案

您可以使用TimeSpan类.

You could use a TimeSpan class.

System.DateTime dt1 =  dateTimePicker1.Value;
System.DateTime dt2 =  dateTimePicker2.Value;
System.TimeSpan diffResult = dt1.Subtract(dt2);


尝试一下:
Try this:
DateTime FirstDate = dateTimePicker1.Value;
DateTime SecondDate = dateTimePicker2.Value;
// Difference in days, hours, and minutes.
TimeSpan ts = SecondDate - FirstDate;
// Difference in days.
int differenceInDays = ts.Days;
Console.WriteLine("Difference in days: {0} ", differenceInDays);




--Amit




--Amit




Hi,

DateTime dt1 = dateTimePicker1.Value;
DateTime dt2 = dateTimePicker2.Value;
TimeSpan tsDiff = dt2.Subtract(dt1); //Note: Always subtract smaller date
//value from greater date
 Console.WriteLine(tsDiff.Days.ToString());



希望对您有帮助



Hope this helps


这篇关于如何计算使用日期时间选择器之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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