计算选定日期的天数 [英] To Calculate number of days with date selected
本文介绍了计算选定日期的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我有两个日历控件,一个用于 From ,另一个用于 To ,从选定的两个日期开始,我必须计算天数,而且还必须排除周六和星期日(即),如果我已申请从星期五到星期一休假,则必须排除周六和周日,并将休假计算为2怎么做
Hi
I have two calendar controls one for From and the other for To and from the two dates selected i have to calculate the number of days and also i have to exclude the saturdays and sundays (i.e) if i have applied for leave from friday till monday I have to exclude sat and sunday and calculate the leave as 2 how to do it
推荐答案
在这里可以获取两个日期之间的总天数
Here You Can Get The Total Number Of Days Between The Two Dates
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() );
对于叶子,您必须编写单独的例程,然后从总天数中减去.
然后,您将获得确切的天数.
For The Leaves You Have to Write A Separate Routine And Then Subtract It From Total Days.
Then You Will Get The Exact Number Of Days.
^ ]
计算营业时间 [
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0625cefa-461b-4a3c-b7f0-d39d06741b70/[^]
Calculating Business Hours[^]
如果日期不存在,人们总是可以迭代...相距不远.此代码段假定没有假期.
One can always iterate... if the dates aren''t too far apart. This code snippet assumes there are no holidays.
using System;
namespace Codeproject.Answers
{
public static class BusinessDaysCounter
{
public static long LeaveDays(DateTime from, DateTime to)
{
if (to < from)
{
return LeaveDays(to, from);
}
long days = 0;
DateTime d = from;
while (d <= to)
{
if (d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
{
days++;
}
d = d.AddDays(1);
}
return days;
}
}
}
这篇关于计算选定日期的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文