string str = "2015年3月";
int firstIndex = str.IndexOf("年", );
int secondIndex = str.IndexOf("月", firstIndex + );
string month = str.Substring(firstIndex + , secondIndex - firstIndex - );
string year = str.Substring(, );
DateTime dt = DateTime.Now;
string[] time = dt.ToString().Split(new char[]{'/'});
string date = year + "/" + month + "/" + time[];
DateTime first = Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day);
DateTime last=Convert.ToDateTime(date).AddDays( - Convert.ToDateTime(date).Day).AddMonths().AddDays(-);
//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 + ;
int fullWeekCount = businessDays / ;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount * )
{
// 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 ? : (int)firstDay.DayOfWeek;
int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? : (int)lastDay.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += ;
if (firstDayOfWeek <= )
{
if (lastDayOfWeek >= )// Both Saturday and Sunday are in the remaining time interval
businessDays -= ;
else if (lastDayOfWeek >= )// Only Saturday is in the remaining time interval
businessDays -= ;
}
else if (firstDayOfWeek <= && lastDayOfWeek >= )// Only Sunday is in the remaining time interval
businessDays -= ;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
return businessDays;
}

C# 根据年月获得此月第一天和最后一天,并计算工作日的更多相关文章

  1. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  2. js 获取某月第一天和最后一天

    1.获取某月第一天和最后一天日期 function getDateByMonth (timeStamp) { let inDate = new Date(timeStamp) let year = i ...

  3. 获取某月第一天,最后一天的sql server脚本 【转】http://blog.csdn.net/chaoowang/article/details/9167969

    这是计算一个月第一天的SQL 脚本:    SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DA ...

  4. 获取某月第一天,最后一天的sql server脚本

    本文来自:http://blog.csdn.net/chaoowang/article/details/9167969 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, ...

  5. C# DateTime 月第一天和最后一天 取法

    取得某月和上个月第一天和最后一天的方法 /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="d ...

  6. asp编程中获取上下两个月第一天和最后一天的代码

    经常在asp编程遇到要获取上个月第一天和最后一天的日期,获取下个月第一天和最后一天的日期.这里总结了一下,将这些asp代码全部列出来了,以便以后遇到的时候使用.    上个月第一天:<%=dat ...

  7. sql 特殊时间值 第一天或最后一天 无计算错误

    DECLARE @dt datetimeSET @dt=GETDATE() DECLARE @number intSET @number=3 --1.指定日期该年的第一天或最后一天--A. 年的第一天 ...

  8. WinForm------DateTime获取月第一天和最后一天取法

    转载: http://blog.csdn.net/livening/article/details/6049341/ 代码: /// <summary> /// 取得某月的第一天 /// ...

  9. java时间计算,获取某月第一天和最后一天

    //获取前月的第一天 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); //获取当前月第一天: Calendar ...

随机推荐

  1. Android清理内存

    Android内存清理,利用ActivityManager获取当前正在运行的进程,清理这些进程释放内存. 可以根据importance的不同来判断前台或后台RunningAppProcessInfo ...

  2. 深入浅出NodeJS——异步I/O

    底层操作系统,异步通过信号量.消息等方式有着广泛的应用. PHP语言从头到尾都是以同步堵塞方式执行,利于程序猿顺序编写业务逻辑. 异步I/O.事件驱动.单线程构成Node的基调. why异步I/O ( ...

  3. Java - 反射机制 2

    package spring.classloader; import java.lang.reflect.Constructor; import java.lang.reflect.Field; im ...

  4. java中List的用法

    list的添加删除等操作 import java.util.*; class TestList { public static void main(String[] args) { List<S ...

  5. HDU 5787 K-wolf Number

    题意:l-r之间有多少个数,其连续k位不存在相同的数字 分析:数位dp,从高位开始向低位进行枚举.因为连续k个数字不相同,在枚举一个数字的时候, 要知道前k-1位的内容,这可以用一个4维的数组表示,再 ...

  6. sql server的两个类型转换函数

    今天遇到一个sql的问题,条件中有个去当前月第一天(2013-8-23 0:00:00),很简单CAST(DATEADD(dd,-DAY(GETDATE())+1,GETDATE()) AS DATE ...

  7. 【Remoting-5代码实现】

    服务端 class RemotingServiceHelper { private static string m_protocolType; private static string urlStr ...

  8. su普通用户切换root用户失败

    http://blog.itpub.net/26432034/viewspace-1688391/ http://blog.csdn.net/zhangdaiscott/article/details ...

  9. ComboGrid 行内点击编辑内容

    最近easyui需要在行内编辑选中项,但是编辑的内容出了当前选中列值,还有其他的,比较麻烦, 先看下这段代码 columns: [[ { field: 'GuestID', title: '编号', ...

  10. python 2.6升级到2.7

    CentOS 6.5上安装的python版本是2.6.6,不能满足我运行软件的要求,所以对python进行升级. 原以为这也就是安装个软件的事儿,在我求稳搜索一下了之后发现,也并不是那么单纯简单. 下 ...