代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
获取星期一,获取第几周
作者:
天哲
/ 发布于
2016/9/13
/
649
protected void Page_Load(object sender, EventArgs e) { DateTime dt1 = Convert.ToDateTime("2012/4/1"); //第一天按周日算 Response.Write(WeekOfMonth(dt1, true) + "<br/>"); //第一天按周一算 Response.Write(WeekOfMonth(dt1, false)+"<br/>"); Response.Write(GetDayOfWeek("2012-04-19", 1)+"<br/>"); Response.Write(GetLastDayOfWeek(2012, 4, 1)+"<br/>"); Response.Write(Convert.ToDateTime("2012-04-01").AddDays(-6)+"<br/>"); Response.Write(GetMonday(Convert.ToDateTime("2012-07-01"))+"<br/>"); Response.Write(GetSundayM(Convert.ToDateTime("2012-05-31")) + "<br/>"); } /// <summary> /// 获取某日期是本月的第几周 /// </summary> /// <param name="dtSel">DateTime 时间对象</param> /// <param name="sundayStart">true:第一天按周日算 false:第一天按周一算</param> /// <returns>获取第几周</returns> public static int WeekOfMonth(DateTime dtSel, bool sundayStart) { //如果要判断的日期为1号,则肯定是第一周了 if (dtSel.Day == 1) return 1; else { //得到本月第一天 DateTime dtStart = new DateTime(dtSel.Year, dtSel.Month, 1); //得到本月第一天是周几 int dayofweek = (int)dtStart.DayOfWeek; //如果不是以周日开始,需要重新计算一下dayofweek,详细DayOfWeek枚举的定义 if (!sundayStart) { dayofweek = dayofweek - 1; if (dayofweek < 0) dayofweek = 7; } //得到本月的第一周一共有几天 int startWeekDays = 7 - dayofweek; //如果要判断的日期在第一周范围内,返回1 if (dtSel.Day <= startWeekDays) return 1; else { int aday = dtSel.Day + 7 - startWeekDays; return aday / 7 + (aday % 7 > 0 ? 1 : 0); } } } /// <summary> /// 输入日期,判断该日期是所在 月、年的第几周 /// </summary> /// <param name="datetype">指定日期类型,"m"表示返回指定日期在所在月的第几周,"y"表示返回指定日期在所在年的第几周</param> /// <param name="curdate">指定的日期 string类型</param> /// <param name="daystart">确定日期计算时间,1表示月、年起始周从第一个星期日开始计算,其他表示默认</param> /// <returns></returns> public static string GetWeek(string datetype, string curdate, int daystart) { if (datetype == "m") { int days = Convert.ToDateTime(curdate).Day; int weeks = 0; int dayofweek = 0; weeks = (days - 1) / 7 + 1; dayofweek = Convert.ToInt32(Convert.ToDateTime(curdate).DayOfWeek); DateTime daydate = new DateTime(Convert.ToDateTime(curdate).Year, Convert.ToDateTime(curdate).Month, 1); int da = System.Convert.ToInt32(daydate.DayOfWeek); if (dayofweek < da) { weeks++; } string startdayofweek = daydate.DayOfWeek.ToString(); if (daystart == 1) { if (startdayofweek == "Sunday") { return weeks.ToString(); } else { weeks = weeks - 1; if (weeks != 0) { return weeks.ToString(); } if (weeks == 0) { curdate = System.Convert.ToDateTime(curdate).AddDays(0 - dayofweek).ToString(); string tmpweekofmonth = GetWeek("m", curdate, 1); return tmpweekofmonth; } return null; } } else { return weeks.ToString(); } return null; } if (datetype == "y") { System.Globalization.CalendarWeekRule startday = new System.Globalization.CalendarWeekRule(); if (daystart == 1) { startday = System.Globalization.CalendarWeekRule.FirstFullWeek; } else { startday = System.Globalization.CalendarWeekRule.FirstDay; } System.Globalization.GregorianCalendar gcyear = new System.Globalization.GregorianCalendar(); int weekofyear = gcyear.GetWeekOfYear(Convert.ToDateTime(curdate), startday, DayOfWeek.Sunday); return weekofyear.ToString(); } return null; } /// <summary> /// 判断某月有多少天 /// </summary> /// <remarks> /// 创建人:zhujt<br/> /// 创建日期:2012-04-19 13:51:07<br/> /// </remarks> /// <param name="year">年</param> /// <param name="month">月</param> /// <returns></returns> public static int GetDay(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return 29; else return 28; default: return 0; } } /// <summary> /// 获取某日是周几 /// </summary> /// <remarks> /// 创建人:zhujt<br/> /// 创建日期:2012-04-19 13:29:58 /// </remarks> /// <param name="date">日期</param> /// <param name="type">显示形式 0:汉字 1:阿拉伯数字</param> /// <returns></returns> public static string GetDayOfWeek(string date, int type) { DateTime dt = Convert.ToDateTime(date); return GetDayOfWeek(type, ref dt); } /// <summary> /// 获取某月最后一周是星期几 /// </summary> /// <param name="year">年</param> /// <param name="month">月</param> /// <param name="type">显示形式 0:汉字 1:阿拉伯数字</param> /// <returns></returns> public static string GetLastDayOfWeek(int year, int month, int type) { DateTime dt = Convert.ToDateTime(year + "-" + month + "-" + GetDay(year, month)); return GetDayOfWeek(1, ref dt); } /// <summary> /// 获取日是周几 /// </summary> /// <remarks> /// 创建人:zhujt<br/> /// 创建日期:2012-04-19 13:29:58 /// </remarks> /// <param name="date">日期</param> /// <param name="type">显示形式 0:汉字 1:阿拉伯数字</param> /// <returns></returns> private static string GetDayOfWeek(int type, ref DateTime dt) { string temp = ""; switch (dt.DayOfWeek) { case DayOfWeek.Sunday: temp = type == 0 ? "星期天" : "7"; break; case DayOfWeek.Monday: temp = type == 0 ? "星期一" : "1"; break; case DayOfWeek.Tuesday: temp = type == 0 ? "星期二" : "2"; break; case DayOfWeek.Wednesday: temp = type == 0 ? "星期三" : "3"; break; case DayOfWeek.Thursday: temp = type == 0 ? "星期四" : "4"; break; case DayOfWeek.Friday: temp = type == 0 ? "星期五" : "5"; break; case DayOfWeek.Saturday: temp = type == 0 ? "星期六" : "6"; break; } return temp; } #region 得到一周的周一和周日的日期 /// <summary> /// 计算本周的周一日期 /// </summary> /// <returns></returns> public static DateTime GetMondayDate() { return GetMondayDate(DateTime.Now); } /// <summary> /// 计算本周周日的日期 /// </summary> /// <returns></returns> public static DateTime GetSundayDate() { return GetSundayDate(DateTime.Now); } /// <summary> /// 计算某日起始日期(礼拜一的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> public static DateTime GetMondayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// <summary> /// 计算某日结束日期(礼拜日的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> public static DateTime GetSundayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); } #endregion /// <summary> /// 从第一周的周一到最后一周的周日 /// 比如2012-04-01 是周日 那么2012年4月第一周周一为2012-03-16 /// 2012-04-30为周一 那么2012-04-30属于五月份 /// 也就是说2012年4月份从2012-03-16到2012-04-29 /// </summary> /// <param name="year">年</param> /// <param name="month">月</param> /// <returns></returns> //public static string[] GetEffective(string year, string month) //{ //} #region 获取有效日期 /// <summary> /// 根据日期获取某周星期一 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetMonday(DateTime dt) { int temp = GetTemp(ref dt); if (temp != 1) // 如果日期不是周一 return dt.AddDays(-temp+1).ToString("yyyy-MM-dd"); else return dt.ToString("yyyy-MM-dd"); } /// <summary> /// 根据日期获取某周星期日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetSundayM(DateTime dt) { int temp = GetTemp(ref dt); if (temp != 7) // 如果日期不是周一 return dt.AddDays(- temp).ToString("yyyy-MM-dd"); else return dt.ToString("yyyy-MM-dd"); } /// <summary> /// 根据日期获取某周星期日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetSunday(DateTime dt) { int temp = GetTemp(ref dt); if (temp != 7) // 如果日期不是周一 return dt.AddDays(7 - temp).ToString("yyyy-MM-dd"); else return dt.ToString("yyyy-MM-dd"); } /// <summary> /// 计算该日期为周几 /// </summary> /// <param name="dt"></param> /// <returns></returns> private static int GetTemp(ref DateTime dt) { int temp = 0; switch (dt.DayOfWeek) { case DayOfWeek.Sunday: temp = 7; break; case DayOfWeek.Monday: temp = 1; break; case DayOfWeek.Tuesday: temp = 2; break; case DayOfWeek.Wednesday: temp = 3; break; case DayOfWeek.Thursday: temp = 4; break; case DayOfWeek.Friday: temp = 5; break; case DayOfWeek.Saturday: temp = 6; break; } return temp; } #endregion
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
天哲
贡献的其它代码
(
13
)
.
JAVA 驼峰与下划线的属性名互相转换
.
获取远程文件HTML
.
判断字符串是否为数值型字符串
.
获取星期一,获取第几周
.
aspx与ascx值传递与设置
.
把同一字段里的多行数据用一行显示
.
固定页脚
.
使用委派代替继承
.
禁用右键单击上下文菜单
.
Jquery- ID与Class之间转换
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3