1、c#获取当前时间是本年的第几周,本月的第几周
private static int getWeekNumInMonth(DateTime daytime) { int dayInMonth = daytime.Day; //本月第一天 DateTime firstDay = daytime.AddDays(1 - daytime.Day); //本月第一天是周几 int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek; //本月第一周有几天 int firstWeekEndDay = 7 - (weekday - 1); //当前日期和第一周之差 int diffday = dayInMonth - firstWeekEndDay; diffday = diffday > 0 ? diffday : 1; //当前是第几周,如果整除7就减一天 int WeekNumInMonth = ((diffday % 7) == 0 ? (diffday / 7 - 1) : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0); return WeekNumInMonth; }
2、获取本年的第几周:
GregorianCalendar gc = new GregorianCalendar(); int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); Console.WriteLine(weekOfYear); Console.ReadLine();
3、c#得到本月有几周和这几周的起止时间示例代码
/// <summary> /// 获得本月有几周 /// </summary> /// <param name="a"></param> /// <returns></returns> public void NumWeeks(DateTime dt) { //年 int year = dt.Year; //月 int month = dt.Month; //当前月第一天 DateTime weekStart = new DateTime(year, month, 1); //该月的最后一天 DateTime monEnd = weekStart.AddMonths(1).AddDays(-1); int i = 1; //当前月第一天是星期几 int dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d")); //该月第一周结束日期 DateTime weekEnd = dayOfWeek == 0 ? weekStart : weekStart.AddDays(7 - dayOfWeek); richTextBox2.Text += "第" + i + "周起始日期: " + weekStart.ToShortDateString() + " 结束日期: " + weekEnd.ToShortDateString() + " "; //当日期小于或等于该月的最后一天 while (weekEnd.AddDays(1) <= monEnd) { i++; //该周的开始时间 weekStart = weekEnd.AddDays(1); //该周结束时间 weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7); richTextBox2.Text += "第" + i + "周起始日期: " + weekStart.ToShortDateString() + " 结束日期: " + weekEnd.ToShortDateString() + " "; } richTextBox2.Text += year + "年" + month + "月共有" + i + "周 "; }
4、My97DatePicker日期插件的常用功能说明
http://jingyan.baidu.com/article/e6c8503c7244bae54f1a18c7.html
以上方法均为实际使用,亲测!!!
总结推荐下
各方法出处省略。