我们可以根据从日期时间选择器控件中选择的日期计算天数吗 [英] Can we calculate days on the basis of dates selected from date time picker control

查看:123
本文介绍了我们可以根据从日期时间选择器控件中选择的日期计算天数吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个酒店管理项目,该项目将根据停留天数来计算账单.
我已经使用日期时间选择器控件将日期和时间以字符串格式插入到我的文本框中

I am trying to build an hotel management project where bill is to be calculated on the basis of number of days stayed.
I have used date time picker control to insert date and time to my textbox into string format

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
       {
      txtdatein.Text = dateTimePicker1.Value.ToString();
      dateTimePicker1.Visible = false;
       }


推荐答案

嗨.

只需要一行.
(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://stackoverflow.com/questions/1607336/calculate-difference-两天之间的天数 [ ^ ]
use timespan

check the following link


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


这篇关于我们可以根据从日期时间选择器控件中选择的日期计算天数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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