C#计算当前日期为一年中的第几周

方法一:

  private   int   WeekOfYear(string   date)

  {

  DateTime   curDay   =   Convert.ToDateTime(date);

  int   firstdayofweek   =   Convert.ToInt32(Convert.ToDateTime(curDay.Year.ToString()   +   "- "   +   "1-1 ").DayOfWeek);

  int   days   =   curDay.DayOfYear;

  int   daysOutOneWeek   =   days   -   (7   -   firstdayofweek);

  if   (daysOutOneWeek   <=   0)

  {

  return   1;

  }

  else

  {

  int   weeks   =   daysOutOneWeek   /   7;

  if   (daysOutOneWeek   %   7   !=   0)

  weeks   ++;

  return   weeks   +   1;

  }

  }

  方法二:

  System.Globalization.GregorianCalendar   gc   =   new   System.Globalization.GregorianCalendar();

  int   weekOfYear   =   gc.GetWeekOfYear(DateTime.Now,   System.Globalization.CalendarWeekRule.FirstDay,   DayOfWeek.Monday);

posted @ 2015-08-28 12:26  kobe工作室  阅读(3811)  评论(0编辑  收藏  举报