除了星期日之外,如何计算两个日期之间的天数? [英] How to calculate a no of days between two dates except sunday?
本文介绍了除了星期日之外,如何计算两个日期之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好先生,
我计算了两天之间的天数,如下所示:
DateTime startdate = new DateTime();
startdate = Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));
DateTime Enddate = new DateTime();
Enddate = Convert.ToDateTime(DateMontYearToYearMonthDate(endate.Text));
TimeSpan点头=(Enddate - startdate);
numdays.Text = nod.TotalDays.ToString();
但是我需要计算没有两个日期之间的天数除了星期日?
如何在c#中执行此操作。如果你回答它会对我这样做更有帮助。提前谢谢。
解决方案
DateTime startdate = Convert.ToDateTime(01/13/2013);
DateTime enddate = Convert.ToDateTime(01/18 / 2013);
int count = 0;
while(startdate!= enddate)
{
if (startdate.DayOfWeek.CompareTo(DayOfWeek.Sunday)!= 0)
{
count ++;
startdate = startdate.AddDays(1) ;
}
其他
{
startdate = startdate.AddDays(1);
}
}
MessageBox.Show(no of days+ count);
这里有一个解决方案:http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-日期 [ ^ ]需要进行一些小改动才能包含星期六 - 它假设工作周为5天,你有6天。
但请你,你不需要这样转换!
startdate = Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));
不要玩文字:看看使用DateTime.TryParseExact [ ^ ] - 它允许您准确指定输入字符串的外观,并在单个操作中转换它(如果用户输入了错误的日期,则报告错误)
Hello Sir,
I have calculated no of days between two days like that given below:
DateTime startdate = new DateTime();
startdate =Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));
DateTime Enddate = new DateTime();
Enddate = Convert.ToDateTime(DateMontYearToYearMonthDate(endate.Text));
TimeSpan nod = (Enddate - startdate);
numdays.Text = nod.TotalDays.ToString();
but i need to calculate no of days between two dates except sunday?
how to do this in c#.if u answer it will more help for me to do this.thanks in advance.
解决方案
DateTime startdate = Convert.ToDateTime("01/13/2013");
DateTime enddate = Convert.ToDateTime("01/18/2013");
int count = 0;
while (startdate != enddate)
{
if (startdate.DayOfWeek.CompareTo(DayOfWeek.Sunday) != 0)
{
count++;
startdate = startdate.AddDays(1);
}
else
{
startdate = startdate.AddDays(1);
}
}
MessageBox.Show("no of days"+count);
There is a solution here: http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates[^] which will need a small change to include Saturdays - it assumes a 5 day working week, where you have a six day.
But please, you don''t need to convert like that!
startdate =Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));
Don''t play with text: look at using DateTime.TryParseExact[^] instead - it lets you specify exactly what the input string should look like, and convert it in a single operation (or report an error if the user entered a bad date).
这篇关于除了星期日之外,如何计算两个日期之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文