C# 根据年月获得此月第一天和最后一天,并计算工作日
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# 根据年月获得此月第一天和最后一天,并计算工作日的更多相关文章
- 用js获取周、月第一天和最后一天(转载)
var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...
- js 获取某月第一天和最后一天
1.获取某月第一天和最后一天日期 function getDateByMonth (timeStamp) { let inDate = new Date(timeStamp) let year = i ...
- 获取某月第一天,最后一天的sql server脚本 【转】http://blog.csdn.net/chaoowang/article/details/9167969
这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一天 SELECT DATEADD(mm, DA ...
- 获取某月第一天,最后一天的sql server脚本
本文来自:http://blog.csdn.net/chaoowang/article/details/9167969 这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, ...
- C# DateTime 月第一天和最后一天 取法
取得某月和上个月第一天和最后一天的方法 /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="d ...
- asp编程中获取上下两个月第一天和最后一天的代码
经常在asp编程遇到要获取上个月第一天和最后一天的日期,获取下个月第一天和最后一天的日期.这里总结了一下,将这些asp代码全部列出来了,以便以后遇到的时候使用. 上个月第一天:<%=dat ...
- sql 特殊时间值 第一天或最后一天 无计算错误
DECLARE @dt datetimeSET @dt=GETDATE() DECLARE @number intSET @number=3 --1.指定日期该年的第一天或最后一天--A. 年的第一天 ...
- WinForm------DateTime获取月第一天和最后一天取法
转载: http://blog.csdn.net/livening/article/details/6049341/ 代码: /// <summary> /// 取得某月的第一天 /// ...
- java时间计算,获取某月第一天和最后一天
//获取前月的第一天 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); //获取当前月第一天: Calendar ...
随机推荐
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 关于JavaScript 原型的理解
原型的含义是指:如果构造器有个原型对象A,则由该构造器创建的实例(Object Instance)都必然复制于A.““在JavaScript中,对象实例(Object Instance)并没有原型,而 ...
- C# 开机启动代码
开机启动/关闭 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- Asp.net MVC razor语法参考
Razor语法的快捷参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ 只是copy下来便于查阅! I ...
- HDU 5727 - Necklace
题意:( 0 <= n <= 9 ) 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环, 已知有些阴珠子和阳珠子不能放在相邻的位置,否则这颗阳珠子就会失去功效, ...
- u-boot基本命令
1.查看环境变量 printenv 2.网络相关命令 设置开发板ip:setenv ipaddr 192.168.2.110 设置子网掩码:setenv netmask 255.255.255.0 设 ...
- C++程序设计实践指导1.6分数运算改写要求实现
改写要求:重载>>和<<实现分数类对象的直接输入输出,重载+完成多个分数对象加法 #include <cstdlib> #include <iostream& ...
- 《C++ Primer Plus 6th》读书笔记 - 第十一章 使用类
1. 运算符重载 2. 计算时间:一个运算符重载示例 3. 友元 1. 友元有三种: 友元函数 友元类 友元成员函数 4. 重载运算符:作为成员函数还是非成员函数 5. 再谈重载:一个矢量类 6. 类 ...
- slf4j 和 log4j使用案例
以Maven项目为例: 步骤: 1.在Maven的pom.xml文件中添加dependency: 之后就会添加3个jar包: 2.在项目下添加log4j.properties 3.log4j.pro ...
- PHP根据身份证号码验证、获取星座、生肖和性别函数
首先介绍一下身份证含义 新的18位身份证号码各位的含义:1-2位省.自治区.直辖市代码:3-4位地级市.盟.自治州代码:5-6位县.县级市.区代码:7-14位出生年月日,比如19670401代表196 ...