如何获得每月的第一个或第二个工作日 [英] How To get First or second weekday of month

查看:143
本文介绍了如何获得每月的第一个或第二个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#DateTeime方法查找月份的第一个或第二个工作日.

how do I find the first or second weekday of month using C# DateTeime method.

推荐答案

相当简单.找到该月的第一天的星期几,检查它是否是工作日,则在日期中添加一天,然后继续进行直到它是工作日.
Fairly simple. Find the day of week of the 1st of the month, check if it is a weekday, it not, add one day to the date and continue doing it until it is a weekday.


尝试这个:
Try this:
public static DateTime? FindMonthWeekDay( int year, int month, int offset )
{
  if ( offset < 1 || offset > DateTime.DaysInMonth( year, month ) )
  {
    throw new ArgumentOutOfRangeException( "offset" );
  }

  DateTime moment = new DateTime( year, month, 1 );
  while ( moment.Month == month )
  {
    DayOfWeek dayOfWeek = moment.DayOfWeek;
    if ( dayOfWeek != DayOfWeek.Saturday && dayOfWeek != DayOfWeek.Sunday )
    {
      offset--;
    }
    if ( offset == 0 )
    {
      return moment;
    }
    moment = moment.AddDays( 1 );
  }
  return null;
} // FindMonthWeekDay


第一天使用offset=1,第二天使用offset=2 ...


Use offset=1 for the first day, offset=2 for the second day...


尝试以下链接.

VB中的平日功能 [获取日期的日期 [
Try to below links.

Weekday Function in VB[^]
Getting Day of the Date[^]


这篇关于如何获得每月的第一个或第二个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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