/// <summary>
/// 年 月 周数 日 字符串数组
/// </summary>
/// <param name="dt">需要转换的日期</param>
/// <returns></returns>
public string[] getYearMonthWeekDayOfDate(DateTime dt)
{
string[] YearMonthWeekDay = new string[4];
YearMonthWeekDay[0] = dt.Year.ToString();
YearMonthWeekDay[1] = dt.Month.ToString();
YearMonthWeekDay[2] = "W" + getWeekNumberOfMonth(dt).ToString();
YearMonthWeekDay[3] = dt.Day.ToString();
return YearMonthWeekDay;
}
/// <summary>
/// 获取日期的周数
/// </summary>
/// <param name="dt">需要转换的日期</param>
/// <returns></returns>
public int getWeekNumberOfMonth(DateTime dt)
{
DateTime monthStartDate = DateTime.Parse(dt.Year.ToString() + "-" + dt.Month.ToString() + "-" + "01");
DateTime monthEndDate = monthStartDate.AddMonths(1);
int difference = getWeekDifference(monthStartDate.DayOfWeek.ToString(), "Sunday");
if (dt <= monthStartDate.AddDays(difference) && dt <= monthEndDate)
{
return 1;
}
else if (dt < monthStartDate.AddDays(difference + 7 + 1) && dt < monthEndDate)
{
return 2;
}
else if (dt < monthStartDate.AddDays(difference + 7 * 2 + 1) && dt < monthEndDate)
{
return 3;
}
else if (dt < monthStartDate.AddDays(difference + 7 * 3 + 1) && dt < monthEndDate)
{
return 4;
}
else if (dt < monthStartDate.AddDays(difference + 7 * 4 + 1) && dt < monthEndDate)
{
return 5;
}
else
{
return 6;
}
}
/// <summary>
/// 结果小于-7或大于7,传入参数错误
/// </summary>
/// <param name="start">起始DayOfWeek</param>
/// <param name="end">结束DayOfWeek</param>
/// <returns></returns>
public int getWeekDifference(string start, string end)
{
int difference = getWeek(end) - getWeek(start);
return difference;
}
/// <summary>
/// DayOfWeek转week
/// </summary>
/// <param name="strWeek">DayOfWeek</param>
/// <returns></returns>
public week getWeek(string strWeek)
{
switch (strWeek)
{
case "Sunday":
return week.Sunday;
case "Monday":
return week.Monday;
case "Tuesday":
return week.Tuesday;
case "Wednesday":
return week.Wednesday;
case "Thursday":
return week.Thursday;
case "Friday":
return week.Friday;
case "Saturday":
return week.Saturday;
default:
return week.Wrong;
}
}
public enum week
{
/// <summary>
/// 星期天
/// </summary>
Sunday = 7,
/// <summary>
/// 星期一
/// </summary>
Monday = 1,
/// <summary>
/// 星期二
/// </summary>
Tuesday = 2,
/// <summary>
/// 星期三
/// </summary>
Wednesday = 3,
/// <summary>
/// 星期四
/// </summary>
Thursday = 4,
/// <summary>
/// 星期五
/// </summary>
Friday = 5,
/// <summary>
/// 星期六
/// </summary>
Saturday = 6,
/// <summary>
///
/// </summary>
Wrong = 0,
}