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 ...
随机推荐
- 一个非常给力的播放器video-js
video-js采用的是html5播放器. 在不支持html5的浏览器会自动切换成flash. video-js的官网http://www.videojs.com/ 看看下载的demo就知道个大概了. ...
- [Python笔记][第三章Python选择与循环]
1月28日学习 Python选择与循环 选择结构 多分枝选择结构 if 表达式 1: 语句块 1 elif 表达式 2: 语句块 2 elif 表达式 3: 语句块 3 ... else : 语句块 ...
- nat网络穿透整理笔记(思维导图)
mindmanger整理的,关于Nat穿透,图片太小,可以点击放大,单独看图片.
- [转]用apache反向代理解决单外网ip对应内网多个web主机的问题
用apache反向代理解决单外网ip对应内网多个web主机的问题 转载一个有独立外网IP,需内网服务器对外发布的例子,是应用apache虚拟主机的. 来源地址:http://www.itshantou ...
- CAShapeLayer + UIBezierPath
UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.使用此类可以定义常见的圆形.多边形等形状 .我们使用直线.弧(arc) ...
- VB.NET 结构(Structure)和类(Class)的区别
类是我们用来构造 VB.NET 应用程序时的最基本的编程结构了. 那结构与类有什么相似之处与不同之处呢? 结构和类, 相同之处是都含有成员,包括构造函数.方法.属性.字段.常量.枚举和事件,都可以实现 ...
- operator 类型转换符
参考脚本之家的这篇博客 http://www.jb51.net/article/41333.htm 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义 ...
- Android studio Debug效率提升
Android studio Debug效率提升,可以在控制台打印log的同时而不暂停程序的运行,尤其是当遇到复杂交互的时候,比如滑动,拖动,这时候程序暂停执行是特别恶心的.其实你可以更新打印信息而不 ...
- python运维开发(十)----IO多路复用线程基本使用
内容目录: python作用域 python2.7和python3.5的多继承区别 IO多路复用 socketserver模块源分析 多线程.进程.协程 python作用域 python中无块级作用 ...
- Lake Counting--poj2386
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23950 Accepted: 12099 D ...