Csharp/C#教程:如何计算从今天起的8个工作日?分享


如何计算从今天起的8个工作日?

我有一个场景,我希望通过从今天的日期起8个工作日来查找日期。 假设今天的日期是04/21/10。现在我想显示日期是04/09 / 10.Weekends应该被排除在外。

例如。 如果今天的日期是04/21/10

减去周末:周六 – 04/10 / 10,04 / 17/10周日-04 / 11 / 10,04 / 18/10

输出结果是04/09/10。

我想用C#做这件事。

任何帮助或建议都会有所帮助。

谢谢,Sumit

你可以在这里找到一些好的解释

https://codeasp.net/blogs/Vijjendra/microsoft-net/765/calculate-business-days-in-asp-net-c

显然有很多方法可以做到这一点,但也许有一些有趣的发电机。 我使用了扩展方法但YMMV。 因此,确定您是否需要使您的代码具有文化意识(或根据您的需要,您需要的任何描述者)等

public static class DateTimeExtensions { public static IEnumerable Forwards(this DateTime dateTime) { return dateTime.Forwards(TimeSpan.FromDays(1)); } public static IEnumerable Forwards(this DateTime dateTime, TimeSpan span) { while (true) { yield return dateTime += span; } } public static IEnumerable Backwards(this DateTime dateTime) { return dateTime.Backwards(TimeSpan.FromDays(1)); } public static IEnumerable Backwards(this DateTime dateTime, TimeSpan span) { return dateTime.Forwards(-span); } public static bool IsWorkingDay(this DateTime dateTime) { return dateTime.IsWorkingDay(Thread.CurrentThread.CurrentUICulture); } public static bool IsWorkingDay(this DateTime dateTime, CultureInfo culture) { return !dateTime.IsWeekend(culture) && !dateTime.IsHoliday(culture); } public static bool IsWeekend(this DateTime dateTime) { return dateTime.IsWeekend(Thread.CurrentThread.CurrentUICulture); } public static bool IsWeekend(this DateTime dateTime, CultureInfo culture) { // TOOD: Make culturally aware return dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday; } public static bool IsHoliday(this DateTime dateTime) { return dateTime.IsHoliday(Thread.CurrentThread.CurrentUICulture); } public static bool IsHoliday(this DateTime dateTime, CultureInfo culture) { throw new NotImplementedException("TODO: Get some culture aware holiday data"); } } 

然后使用DateTime生成器为一些LINQ表达式提供动力:

  // Display every holiday from today until the end of the year DateTime.Today.Forwards() .TakeWhile(date => date.Year <= DateTime.Today.Year) .Where(date => date.IsHoliday()) .ForEach(date => Console.WriteLine(date)); 

你得到了照片

看看这篇文章它可能对你有用

https://www.codeproject.com/KB/cs/busdatescalculation.aspx

这是非常愚蠢的算法,但它将工作8天。 如果开始日是周一至周三(包括),则添加10天,否则为12天。 更一般的是循环添加一天,检查是否是工作日。

只有七种情况需要考虑,所以我会根据星期几来计算减去实际天数,这样的事情(显然不完整或经过测试):

 switch (dayOfWeek) { case DayOfWeek.Monday : case DayOfWeek.Tuesday : case DayOfWeek.Wednesday : return 12; case DayOfWeek.Thursday : case DayOfWeek.Friday : case DayOfWeek.Saturday : return 10; case DayOfWeek.Sunday : return 11; } 

 var desiredDate = DateTime.Now.SubtractBusinessDays(8); 

在Codeplex上使用Fluent DateTime项目。

上述就是C#学习教程:如何计算从今天起的8个工作日?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

  static DateTime GetBusinessDay(int days) { var dateTime = DateTime.Now; bool run = true; int i = 0; while (run) { dateTime = dateTime.AddDays(1); if (dateTime.DayOfWeek == DayOfWeek.Saturday || dateTime.DayOfWeek == DayOfWeek.Sunday) { continue; } i++; if (i == 10) { run = false; } } return dateTime; } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1009721.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐