• C# 根据年月获得此月第一天和最后一天,并计算工作日


    string str = "2015年3月";
    int firstIndex = str.IndexOf("", 0);
    int secondIndex = str.IndexOf("", firstIndex + 1);
    string month = str.Substring(firstIndex + 1, secondIndex - firstIndex - 1);
    string year = str.Substring(0, 4);
    DateTime dt = DateTime.Now;
    string[] time = dt.ToString().Split(new char[]{'/'});
    string date = year + "/" + month + "/" + time[2];
    DateTime first = Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day);
    DateTime last=Convert.ToDateTime(date).AddDays(1 - Convert.ToDateTime(date).Day).AddMonths(1).AddDays(-1);
    //The first day of the month
    Label1.Text = first.ToString();
    //The last day of the month
    Label2.Text = last.ToString();
    //businessDays of the month(except on the weekend)
    Label3.Text = getint(first, last).ToString();

    getint 方法:

    public int getint(DateTime firstDay, DateTime lastDay)
    {
        firstDay = firstDay.Date;
        lastDay = lastDay.Date;
        if (firstDay > lastDay)
            throw new ArgumentException("Incorrect last day " + lastDay);
        TimeSpan span = lastDay - firstDay;
        int businessDays = span.Days + 1;
        int fullWeekCount = businessDays / 7;
        // find out if there are weekends during the time exceedng the full weeks
        if (businessDays > fullWeekCount * 7)
        {
            // we are here to find out if there is a 1-day or 2-days weekend
            // in the time interval remaining after subtracting the complete weeks
            int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)firstDay.DayOfWeek;
            int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)lastDay.DayOfWeek;
    
            if (lastDayOfWeek < firstDayOfWeek)
                lastDayOfWeek += 7;
            if (firstDayOfWeek <= 6)
            {
                if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
                    businessDays -= 2;
                else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
                    businessDays -= 1;
            }
            else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
                businessDays -= 1;
        }
        // subtract the weekends during the full weeks in the interval
        businessDays -= fullWeekCount + fullWeekCount;
        return businessDays;
    }
  • 相关阅读:
    团购的玩法 要粘性也要乐趣
    风讯CMS提交时出现System.Web.HttpRequestValidationException (0x80004005):错误
    关于传统零售企业网上经营模式的探讨
    一个很漂亮的转入别的页面时等待页
    采集百度图片
    带有立体感的凹陷字体,非常夺人眼球
    sql2005 远程连接问题解决方法
    草根创业:接纳与淘汰终归“剩”者为王
    写一个属于自己的模板引擎(1)
    写一个属于自己的模板引擎(2)
  • 原文地址:https://www.cnblogs.com/Tinoloving/p/4682887.html
Copyright © 2020-2023  润新知