#region 根据条件获取第一天和最后一天
/// <summary>
/// 获取传入日期在当前月是第几周(公历计算)
/// 第一周不足算一周
/// </summary>
/// <param name="dt">一个日期对象</param>
/// <returns>计算日期所在月为第几周</returns>
public static int GetWeekIndexOfMonth(DateTime dt)
{
//当天所在的年份
int year = dt.Year;
int month = dt.Month;
//当月的第一天
DateTime firstDay = new DateTime(year, month, 1);
//当月的第一天是星期几
int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);
//当月第一周的天数
int firstWeekDayNum = 7 - firstOfWeek;
//传入日期在当月的天数与第一周天数的差
int otherDays = dt.Day - firstWeekDayNum;
//传入日期不在第一周内
if (otherDays > 0)
{
int weekNumOfOtherDays;
if (otherDays % 7 == 0)
{
weekNumOfOtherDays = otherDays / 7;
}
else
{
weekNumOfOtherDays = otherDays / 7 + 1;
}
return weekNumOfOtherDays + 1;
}
else//传入日期在第一周内
{
return 1;
}
}
/// <summary>
/// 获取传入日期在当前年是第几周(公历计算)
/// 第一周不足算一周
/// </summary>
/// <param name="dt">一个日期对象</param>
/// <returns>计算日期所在年为第几周</returns>
public static int GetWeekIndexOfYear(DateTime dt)
{
//当天所在的年份
var year = dt.Year;
//当年的第一天
var firstDay = new DateTime(year, 1, 1);
//当年的第一天是星期几
int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);
//当年第一周的天数
int firstWeekDayNum = 7 - firstOfWeek;
//传入日期在当年的天数与第一周天数的差
int otherDays = dt.DayOfYear - firstWeekDayNum;
//传入日期不在第一周内
if (otherDays > 0)
{
int weekNumOfOtherDays;
if (otherDays % 7 == 0)
{
weekNumOfOtherDays = otherDays / 7;
}
else
{
weekNumOfOtherDays = otherDays / 7 + 1;
}
return weekNumOfOtherDays + 1;
}
else//传入日期在第一周内
{
return 1;
}
}
/// <summary>
/// 获取季号
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static int GetSeasonIndexOfYear(DateTime dt)
{
var mon = dt.Month;
if( mon >=1 && mon <=3)
{
return 1;
}
else if (mon >= 4 && mon <= 6)
{
return 2;
}
else if (mon >= 7 && mon <= 9)
{
return 3;
}
else
{
return 4;
}
}
/// <summary>
/// 获取半年号
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static int GetHalfYearIndexOfYear(DateTime dt)
{
var mon = dt.Month;
if (mon >= 1 && mon <= 6)
{
return 1;
}
else
{
return 2;
}
}
/// <summary>
/// 获取每年第一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYear(int year)
{
return new DateTime(year, 1, 1);
}
/// <summary>
/// 获取每年最后一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetEndDateByYear(int year)
{
return new DateTime(year + 1, 1, 1).AddDays(-1);
}
/// <summary>
/// 获取每半年第一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndHalfYear(int year,int byIndex)
{
if (byIndex == 1)
{
return new DateTime(year, 1, 1);
}
else
{
return new DateTime(year, 6, 1);
}
}
/// <summary>
/// 获取每半年年最后一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndHalfYear(int year, int byIndex)
{
if (byIndex ==1)
{
return new DateTime(year, 6, 1);
}
else
{
return new DateTime(year + 1, 1, 1).AddDays(-1);
}
}
/// <summary>
/// 获取每月第一天
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndMonth(int year,int month)
{
return new DateTime(year, month, 1);
}
/// <summary>
/// 获取每月最后一天
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndMonth(int year, int month)
{
return new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
}
/// <summary>
/// 获取季度第一天
/// </summary>
/// <param name="year"></param>
/// <param name="season"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndSeason(int year, int season)
{
DateTime ret = new DateTime(year, 1, 1);
if (season >= 1 && season <= 4)
{
switch (season)
{
case 1:
ret = new DateTime(year, 1, 1);
break;
case 2:
ret = new DateTime(year, 4, 1);
break;
case 3:
ret = new DateTime(year, 7, 1);
break;
case 4:
ret = new DateTime(year, 11, 1);
break;
}
}
else
{
throw new Exception("季度号异常");
}
return ret;
}
/// <summary>
/// 获取季度最后一天
/// </summary>
/// <param name="year"></param>
/// <param name="season"></param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndSeason(int year, int season)
{
DateTime ret = new DateTime(year, 3, 31);
if (season >= 1 && season <= 4)
{
switch (season)
{
case 1:
ret = new DateTime(year, 3, 31);
break;
case 2:
ret = new DateTime(year, 6, 30);
break;
case 3:
ret = new DateTime(year, 9, 30);
break;
case 4:
ret = new DateTime(year, 12, 31);
break;
}
}
else
{
throw new Exception("季度号异常");
}
return ret;
}
/// <summary>
/// 根据年份和周数获取日期(该周第一天)
/// </summary>
/// <param name="year"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetBeginDateByWeekNumAndYear(int year,int weekindex)
{
DateTime firstDay = new DateTime(year, 1, 1);
if (weekindex <1)
{
throw new Exception("入参周次有误!");
}
if (weekindex == 1)
{
return firstDay;
}
else
{
firstDay = firstDay.AddDays((weekindex - 1)*7);
firstDay = firstDay.AddDays(-Convert.ToInt32(firstDay.DayOfWeek));
if (firstDay.Year == year)
{
return firstDay;
}
else
{
throw new Exception("入参周次有误!");
}
}
}
/// <summary>
/// 根据年份和周数获取日期(该周最后一天)
/// </summary>
/// <param name="year"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetEndDateByWeekNumAndYear(int year, int weekindex)
{
var firstDay = GetBeginDateByWeekNumAndYear(year, weekindex);
var LastDay = firstDay.AddDays(6);
if (LastDay.Year == year+ 1)
{
LastDay = new DateTime(year,12, 31);
}
return LastDay;
}
/// <summary>
/// 根据年份 月份和周数获取日期(该周第一天)
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetBeginDateByWeekNumAndYearAndMonth(int year, int month, int weekindex)
{
//第一天
DateTime firstDay = new DateTime(year, month, 1);
if (weekindex < 1)
{
throw new Exception("入参周次有误!");
}
if (weekindex == 1)
{
return firstDay;
}
else
{
firstDay = firstDay.AddDays((weekindex - 1) * 7);
firstDay = firstDay.AddDays(-Convert.ToInt32(firstDay.DayOfWeek));
if (firstDay.Year == year && firstDay.Month == month)
{
return firstDay;
}
else
{
throw new Exception("入参周次有误!");
}
}
}
/// <summary>
/// 根据年份 月份和周数获取日期(该周最后一天)
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetEndDateByWeekNumAndYearAndMonth(int year,int month, int weekindex)
{
//第一天
var firstDay = GetBeginDateByWeekNumAndYearAndMonth(year, month, weekindex);
var LastDay = firstDay.AddDays(6);
if(LastDay.Month != month)
{
if (LastDay.Year == year + 1)
{
LastDay = new DateTime(year, 12, 31);
}
else
{
LastDay = new DateTime(year, month+1, 1).AddDays(-1);
}
}
return LastDay;
}
#endregion 根据条件获取第一天和最后一天
/// <summary>
/// 获取传入日期在当前月是第几周(公历计算)
/// 第一周不足算一周
/// </summary>
/// <param name="dt">一个日期对象</param>
/// <returns>计算日期所在月为第几周</returns>
public static int GetWeekIndexOfMonth(DateTime dt)
{
//当天所在的年份
int year = dt.Year;
int month = dt.Month;
//当月的第一天
DateTime firstDay = new DateTime(year, month, 1);
//当月的第一天是星期几
int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);
//当月第一周的天数
int firstWeekDayNum = 7 - firstOfWeek;
//传入日期在当月的天数与第一周天数的差
int otherDays = dt.Day - firstWeekDayNum;
//传入日期不在第一周内
if (otherDays > 0)
{
int weekNumOfOtherDays;
if (otherDays % 7 == 0)
{
weekNumOfOtherDays = otherDays / 7;
}
else
{
weekNumOfOtherDays = otherDays / 7 + 1;
}
return weekNumOfOtherDays + 1;
}
else//传入日期在第一周内
{
return 1;
}
}
/// <summary>
/// 获取传入日期在当前年是第几周(公历计算)
/// 第一周不足算一周
/// </summary>
/// <param name="dt">一个日期对象</param>
/// <returns>计算日期所在年为第几周</returns>
public static int GetWeekIndexOfYear(DateTime dt)
{
//当天所在的年份
var year = dt.Year;
//当年的第一天
var firstDay = new DateTime(year, 1, 1);
//当年的第一天是星期几
int firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek);
//当年第一周的天数
int firstWeekDayNum = 7 - firstOfWeek;
//传入日期在当年的天数与第一周天数的差
int otherDays = dt.DayOfYear - firstWeekDayNum;
//传入日期不在第一周内
if (otherDays > 0)
{
int weekNumOfOtherDays;
if (otherDays % 7 == 0)
{
weekNumOfOtherDays = otherDays / 7;
}
else
{
weekNumOfOtherDays = otherDays / 7 + 1;
}
return weekNumOfOtherDays + 1;
}
else//传入日期在第一周内
{
return 1;
}
}
/// <summary>
/// 获取季号
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static int GetSeasonIndexOfYear(DateTime dt)
{
var mon = dt.Month;
if( mon >=1 && mon <=3)
{
return 1;
}
else if (mon >= 4 && mon <= 6)
{
return 2;
}
else if (mon >= 7 && mon <= 9)
{
return 3;
}
else
{
return 4;
}
}
/// <summary>
/// 获取半年号
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static int GetHalfYearIndexOfYear(DateTime dt)
{
var mon = dt.Month;
if (mon >= 1 && mon <= 6)
{
return 1;
}
else
{
return 2;
}
}
/// <summary>
/// 获取每年第一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYear(int year)
{
return new DateTime(year, 1, 1);
}
/// <summary>
/// 获取每年最后一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetEndDateByYear(int year)
{
return new DateTime(year + 1, 1, 1).AddDays(-1);
}
/// <summary>
/// 获取每半年第一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndHalfYear(int year,int byIndex)
{
if (byIndex == 1)
{
return new DateTime(year, 1, 1);
}
else
{
return new DateTime(year, 6, 1);
}
}
/// <summary>
/// 获取每半年年最后一天
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndHalfYear(int year, int byIndex)
{
if (byIndex ==1)
{
return new DateTime(year, 6, 1);
}
else
{
return new DateTime(year + 1, 1, 1).AddDays(-1);
}
}
/// <summary>
/// 获取每月第一天
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndMonth(int year,int month)
{
return new DateTime(year, month, 1);
}
/// <summary>
/// 获取每月最后一天
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndMonth(int year, int month)
{
return new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
}
/// <summary>
/// 获取季度第一天
/// </summary>
/// <param name="year"></param>
/// <param name="season"></param>
/// <returns></returns>
public static DateTime GetBeginDateByYearAndSeason(int year, int season)
{
DateTime ret = new DateTime(year, 1, 1);
if (season >= 1 && season <= 4)
{
switch (season)
{
case 1:
ret = new DateTime(year, 1, 1);
break;
case 2:
ret = new DateTime(year, 4, 1);
break;
case 3:
ret = new DateTime(year, 7, 1);
break;
case 4:
ret = new DateTime(year, 11, 1);
break;
}
}
else
{
throw new Exception("季度号异常");
}
return ret;
}
/// <summary>
/// 获取季度最后一天
/// </summary>
/// <param name="year"></param>
/// <param name="season"></param>
/// <returns></returns>
public static DateTime GetEndDateByYearAndSeason(int year, int season)
{
DateTime ret = new DateTime(year, 3, 31);
if (season >= 1 && season <= 4)
{
switch (season)
{
case 1:
ret = new DateTime(year, 3, 31);
break;
case 2:
ret = new DateTime(year, 6, 30);
break;
case 3:
ret = new DateTime(year, 9, 30);
break;
case 4:
ret = new DateTime(year, 12, 31);
break;
}
}
else
{
throw new Exception("季度号异常");
}
return ret;
}
/// <summary>
/// 根据年份和周数获取日期(该周第一天)
/// </summary>
/// <param name="year"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetBeginDateByWeekNumAndYear(int year,int weekindex)
{
DateTime firstDay = new DateTime(year, 1, 1);
if (weekindex <1)
{
throw new Exception("入参周次有误!");
}
if (weekindex == 1)
{
return firstDay;
}
else
{
firstDay = firstDay.AddDays((weekindex - 1)*7);
firstDay = firstDay.AddDays(-Convert.ToInt32(firstDay.DayOfWeek));
if (firstDay.Year == year)
{
return firstDay;
}
else
{
throw new Exception("入参周次有误!");
}
}
}
/// <summary>
/// 根据年份和周数获取日期(该周最后一天)
/// </summary>
/// <param name="year"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetEndDateByWeekNumAndYear(int year, int weekindex)
{
var firstDay = GetBeginDateByWeekNumAndYear(year, weekindex);
var LastDay = firstDay.AddDays(6);
if (LastDay.Year == year+ 1)
{
LastDay = new DateTime(year,12, 31);
}
return LastDay;
}
/// <summary>
/// 根据年份 月份和周数获取日期(该周第一天)
/// </summary>
/// <param name="year"></param>
/// <param name="month"></param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetBeginDateByWeekNumAndYearAndMonth(int year, int month, int weekindex)
{
//第一天
DateTime firstDay = new DateTime(year, month, 1);
if (weekindex < 1)
{
throw new Exception("入参周次有误!");
}
if (weekindex == 1)
{
return firstDay;
}
else
{
firstDay = firstDay.AddDays((weekindex - 1) * 7);
firstDay = firstDay.AddDays(-Convert.ToInt32(firstDay.DayOfWeek));
if (firstDay.Year == year && firstDay.Month == month)
{
return firstDay;
}
else
{
throw new Exception("入参周次有误!");
}
}
}
/// <summary>
/// 根据年份 月份和周数获取日期(该周最后一天)
/// </summary>
/// <param name="year"></param>
/// <param name="month"> </param>
/// <param name="weekindex"></param>
/// <returns></returns>
public static DateTime GetEndDateByWeekNumAndYearAndMonth(int year,int month, int weekindex)
{
//第一天
var firstDay = GetBeginDateByWeekNumAndYearAndMonth(year, month, weekindex);
var LastDay = firstDay.AddDays(6);
if(LastDay.Month != month)
{
if (LastDay.Year == year + 1)
{
LastDay = new DateTime(year, 12, 31);
}
else
{
LastDay = new DateTime(year, month+1, 1).AddDays(-1);
}
}
return LastDay;
}
#endregion 根据条件获取第一天和最后一天