You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.2 KiB
58 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ZXLHelp
|
|
{
|
|
public static class ZXLHelp
|
|
{
|
|
/// <summary>
|
|
/// 扩展方法,时间是这个月第几周
|
|
/// </summary>
|
|
/// <param name="daytime"></param>
|
|
/// <returns></returns>
|
|
public static int getWeekNumInMonth(this 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 扩展方法,时间是这个年第几周
|
|
/// </summary>
|
|
/// <param name="daytime"></param>
|
|
/// <returns></returns>
|
|
public static int getWeekNumInYear(this DateTime daytime,int BeginWeek)
|
|
{
|
|
int dayInYear = daytime.Year;
|
|
//本年第一天
|
|
DateTime firstDay = DateTime.Parse(dayInYear.ToString() + "-01-01");
|
|
//本年第一天是周几,0是周日
|
|
int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
|
|
//本年第一周是哪儿天开始的
|
|
int firstWeekBeginDay = 7 - (weekday - 1);
|
|
//当前日期和第一周之差
|
|
int diffday = dayInYear - firstWeekEndDay;
|
|
diffday = diffday > 0 ? diffday : 1;
|
|
//当前是第几周,如果整除7就减一天
|
|
int WeekNumInMonth = ((diffday % 7) == 0
|
|
? (diffday / 7 - 1)
|
|
: (diffday / 7)) + 1 + (dayInYear > firstWeekEndDay ? 1 : 0);
|
|
return WeekNumInMonth;
|
|
}
|
|
}
|
|
}
|
|
|