本文整理汇总了C#中DayOfWeek.Any方法的典型用法代码示例。如果您正苦于以下问题:C# DayOfWeek.Any方法的具体用法?C# DayOfWeek.Any怎么用?C# DayOfWeek.Any使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DayOfWeek
的用法示例。
在下文中一共展示了DayOfWeek.Any方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsBusinessDay
/// <summary>
/// Determines if the <paramref name="dateTime" /> date parameter is a business day based on the supplied
/// <paramref name="weekendDays" /> and <paramref name="holidays" />
/// </summary>
/// <param name="dateTime">The DateTime to check</param>
/// <param name="weekendDays">
/// The days of the week to consider as the weekend. These days of the week will be excluded when
/// determining business days.
/// </param>
/// <param name="holidays">Holidays dates to exclude when determining business days</param>
/// <returns>True if the <paramref name="dateTime" /> is a business day</returns>
public static bool IsBusinessDay(DateTime dateTime, DayOfWeek[] weekendDays, params DateTime[] holidays)
=> !(weekendDays.Any(weekendDay => weekendDay == dateTime.DayOfWeek) || holidays.Any(holiday => holiday.Date == dateTime.Date));
示例2: BusinessDays
/// <summary>
/// Determines the number of business days between two dates
/// </summary>
/// <param name="start">The DateTime to use as the start when determining the difference</param>
/// <param name="end">The DateTime to use as the end when determining the difference</param>
/// <param name="weekendDays">
/// The days of the week to consider as the weekend. These days of the week will be excluded when
/// determining business days.
/// </param>
/// <param name="holidays">Holidays dates to exclude when determining business days</param>
/// <returns>The number of business days between the <paramref name="start" /> and <paramref name="end" /> dates</returns>
public static int BusinessDays(DateTime start, DateTime end, DayOfWeek[] weekendDays, params DateTime[] holidays)
{
start = start.Date;
end = end.Date;
if (start > end)
throw new ArgumentException($"End date must be greater than {start:F}", nameof(end));
if (weekendDays == null || !weekendDays.Any())
throw new ArgumentException("Weekend Days is null or empty");
var lastWeekendDay = weekendDays.Last();
var span = end - start;
var businessDays = span.Days + 1;
var fullWeekCount = businessDays/7;
if (businessDays > fullWeekCount*7)
{
var startDayOfWeek = start.DayOfWeek == lastWeekendDay
? 7
: (int)start.DayOfWeek;
var endDayOfWeek = end.DayOfWeek == lastWeekendDay
? 7
: (int)end.DayOfWeek;
if (endDayOfWeek < startDayOfWeek)
endDayOfWeek += 7;
if (startDayOfWeek <= 6)
{
if (endDayOfWeek >= 7)
businessDays -= 2;
else if (endDayOfWeek >= 6)
businessDays -= 1;
}
else if (startDayOfWeek <= 7 && endDayOfWeek >= 7)
businessDays -= 1;
}
businessDays -= (from holiday in holidays
where holiday.Date >= start && holiday.Date <= end && weekendDays.Any(weekendDay => weekendDay != holiday.DayOfWeek)
select holiday).Count() + 2*fullWeekCount;
return businessDays;
}