本文介绍了是上个工作日吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


以下c#代码告诉您日期是否为该月的最后一天.
问题:
如何获得每月的最后一个工作日?
例如,对于3月,最后一天是星期六,即31日.但我希望代码返回3月30日,这是3月的最后一个工作日.
谢谢

Hi,
The following c# code tells you if the date is the last day of the month.
Question:
How can I get the last WORKING day of the month?
for example, for march, th elast day is saturday which is 31st. But I want the code to return 30th which is the last working day of march.
Thanks

private bool IsLastDayOfMonth(DateTime dtDate)
        {
            DateTime dtTo = dtDate.Date;
            dtTo = dtTo.AddMonths(1);
            // remove all of the days in the next month 
            // to get bumped down to the last day of the 
            // previous month 
            dtTo = dtTo.AddDays(-(dtTo.Day));

            if (dtDate.Date == dtTo)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

推荐答案

// ----------------------------------------------------------------------
private static DateTime LastDayOfMonth( int year, int month )
{
  return new DateTime( year, month, DateTimeFormatInfo.CurrentInfo.Calendar.GetDaysInMonth( year, month ) );
} // LastDayOfMonth

// ----------------------------------------------------------------------
private static bool IsLastDayOfMonth( DateTime test )
{
  DateTime lastDayOfMonth = LastDayOfMonth( test.Year, test.Month );
  return test.Day == lastDayOfMonth.Day;
} // IsLastDayOfMonth

// ----------------------------------------------------------------------
private static bool IsLastWorkingDayOfMonth( DateTime test )
{
  DateTime lastWorkingDayOfMonth = LastDayOfMonth( test.Year, test.Month );
  while ( lastWorkingDayOfMonth.DayOfWeek == DayOfWeek.Saturday ||
    lastWorkingDayOfMonth.DayOfWeek == DayOfWeek.Sunday )
  {
    lastWorkingDayOfMonth = lastWorkingDayOfMonth.AddDays( -1 );
  }

  return test.Day == lastWorkingDayOfMonth.Day;
} // IsLastWorkingDayOfMonth


这篇关于是上个工作日吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 12:10