日期计算在c# [英] Date calculation in c#

查看:126
本文介绍了日期计算在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(c#)中,我需要将 n 天添加到今天的日期。我知道我可以使用 DateTime.AddDays(n)方法,这种方法工作正常。但在我的情况下,我只想添加工作日(或只添加星期一和星期五或任何其他集合)。

In my app (c#) I need to add n days to today's date. I know that I can use DateTime.AddDays(n) method, and this method works fine. But in my situation I want to add only working days (or only "Mondays" and "Fridays" or any other sets).

可能存在任何默认方法来计算此类逻辑。

Maybe exist any default methods to calculate such type of logic.

测试数据:

日期:今天(2014年6月24日)。

添加日期:10

日期类型:工作日(Mn-Fr)

答案:8-july-2014

Date: Today (24-jun-2014).
Days to add: 10
Days type: Business days (Mn-Fr)
Answer: 8-july-2014

推荐答案

您可以使用LINQ:

DayOfWeek[] weekEnd = { DayOfWeek.Saturday, DayOfWeek.Sunday };
DateTime end = Enumerable.Range(0, int.MaxValue)
            .Select(i => DateTime.Today.AddDays(i))
            .Where(d => !weekEnd.Contains(d.DayOfWeek))
            .Take(10)
            .Last();

然而,它返回07/07,我假设因为它包括今天。我不知道是否需要。如果你不想今天将 Range(0,int.MaxValue)更改为 Range(1,int.MaxValue)

However, it returns 07/07, i assume because it includes today. I don't know if it is desired. If you don't want to include today change Range(0, int.MaxValue) to Range(1, int.MaxValue).

这篇关于日期计算在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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