close
Calendar.GetWeekOfYear:計算日期位於一年中第幾個星期
CalendarWeekRule 列舉型別:定義決定年份的第一週的各種規則 (Rule)
FirstDay:指示一年的第一週開始於該年的第一天,並結束於被指定為該週第一天的前一天。
FirstFullWeek:指示一年的第一週開始於,一年的第一天當天或之後被指定為一週第一天的那天。
FirstFourDayWeek:指示一年的第一週有四天以上在被指定為該週的第一天之前。
DayOfWeek 列舉型別:用來指定一週的第一天是星期幾
1: public static int GetWeekNumber(DateTime date)
2: {
3: var ci = CultureInfo.CurrentCulture;
4: return ci.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
5: }
--
引用自:瓶水相逢.Net
==========================================================
private int DatePart(System.DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);//今天星期幾
int daydiff = (-1) * (weeknow+1);//今日與上周末的天數差
int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第幾天
int weeks = days/7;
if(days%7 != 0)
{
weeks++;
}
//此時,weeks為上周是本年的第幾周
return (weeks+1);
}
//本周起止日期
private string WeekRange(System.DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);
int daydiff = (-1) * weeknow;
int dayadd = 6-weeknow;
string dateBegin = System.DateTime.Now.AddDays(daydiff).Date.ToString("MM月dd日");
string dateEnd = System.DateTime.Now.AddDays(dayadd).Date.ToString("MM月dd日");
return dateBegin + " - " +dateEnd;
}
//計算第幾周(重新修改后)
public int GetWeekOfCurrDate(DateTime dt)
{
int Week = 1;
int nYear = dt.Year;
System.DateTime FirstDayInYear = new DateTime(nYear, 1, 1);
System.DateTime LastDayInYear = new DateTime(nYear, 12, 31);
int DaysOfYear = Convert.ToInt32(LastDayInYear.DayOfYear);
int WeekNow = Convert.ToInt32(FirstDayInYear.DayOfWeek) - 1;
if (WeekNow < 0) WeekNow = 6;
int DayAdd = 6 - WeekNow;
System.DateTime BeginDayOfWeek = new DateTime(nYear, 1, 1);
System.DateTime EndDayOfWeek = BeginDayOfWeek.AddDays(DayAdd);
Week = 2;
for (int i = DayAdd + 1; i <= DaysOfYear; i++)
{
BeginDayOfWeek = FirstDayInYear.AddDays(i);
if (i + 6 > DaysOfYear)
{
EndDayOfWeek = BeginDayOfWeek.AddDays(DaysOfYear - i - 1);
}
else
{
EndDayOfWeek = BeginDayOfWeek.AddDays(6);
}
if (dt.Month == EndDayOfWeek.Month && dt.Day <= EndDayOfWeek.Day)
{
break;
}
Week++;
i = i + 6;
}
return Week;
}
--
全站熱搜
留言列表