.NET日期比较:计算的工作日数量,因为一个日期? [英] .NET Date Compare: Count the amount of working days since a date?

查看:344
本文介绍了.NET日期比较:计算的工作日数量,因为一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是计算工作日的量,因为一个日期最简单的方法? VB.NET preferred,但C#是好的。

What's the easiest way to compute the amount of working days since a date? VB.NET preferred, but C# is okay.

和以工作日,我的意思是所有的日子不包括星期六和星期天。如果该算法还可以考虑到具体的排斥的日期不应该算作工作日内名单,这将是肉汁。

And by "working days", I mean all days excluding Saturday and Sunday. If the algorithm can also take into account a list of specific 'exclusion' dates that shouldn't count as working days, that would be gravy.

在此先感谢了贡献的天才。

Thanks in advance for the contributed genius.

推荐答案

这会做你想要的。它应该很容易转换到VB.NET,它已经太长,我能,虽然做到这一点。

This'll do what you want it to. It should be easy enough to convert to VB.NET, it's been too long for me to be able to do it though.

DateTime start = DateTime.Now;
DateTime end = start.AddDays(9);
IEnumerable<DateTime> holidays = new DateTime[0];

// basic data
int days = (int)(end - start).TotalDays;
int weeks = days / 7;

// check for a weekend in a partial week from start.
if (7- (days % 7) <= (int)start.DayOfWeek)
    days--;
if (7- (days % 7) <= (int)start.DayOfWeek)
    days--;

// lose the weekends
days -= weeks * 2;

foreach (DateTime dt in holidays)
{
    if (dt > start && dt < end)
        days--;
}

这篇关于.NET日期比较:计算的工作日数量,因为一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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