且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们可以根据从日期时间选择器控件中选择的日期计算天数吗

更新时间:2023-01-31 18:15:33

嗨.

只需要一行.
(toDate-fromDate).天数



使用类型为"TimeSpan"的对象可以轻松完成此操作.例如:假设我们想知道最大值与最大值之间的天数.和分钟. DateTime Type的值并将其显示在控制台窗口中,那么我可以编写类似以下内容的内容:

DateTime d1 = DateTime.MinValue;
DateTime d2 = DateTime.MaxValue;
TimeSpan span = d2-d1;
Console.WriteLine
("{1}和{2}之间有{0}天,span.TotalDays,d1.ToString(),d2.ToString());
请注意,我使用TotalDays属性获取之间的天数.这使我获得考虑天数与小数天数(366天的年份)的天数.考虑到所有年份仅包含365天,我还可以使用属性"Days"来获得价值.

请参阅此链接
http://r4r.co.in/c1/01/tutorial/csharp/Calculating%20Duration%20Between%20Two%20Dates%20in%20Years,%20Months%20and%20Days.shtml [
Hi.

only takes one line.
(toDate - fromDate).Days

or

This can be easily accomplished using an object of Type "TimeSpan". For example: let''s assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:

DateTime d1=DateTime.MinValue;
DateTime d2=DateTime.MaxValue;
TimeSpan span=d2-d1;
Console.WriteLine
( "There''re {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );
Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.

refer this link
http://r4r.co.in/c1/01/tutorial/csharp/Calculating%20Duration%20Between%20Two%20Dates%20in%20Years,%20Months%20and%20Days.shtml[^]


使用时间跨度

检查以下链接


http://***.com/questions/1607336/calculate-difference-两天之间的天数 [ ^ ]
use timespan

check the following link


http://***.com/questions/1607336/calculate-difference-between-two-dates-number-of-days[^]