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