首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

计算某一天在一年中是第几周

计算某一天在一年中是第几周

//一年中的第几周
    //wYear wMonth wDay 年月日
    //bSundayAsFirstDay 星期天作为第一天
    //返回 1~53
    int GetWeeksOfYear(int wYear, int wMonth, int wDay, BOOL bMondayAsFirstDay)
    {
      int iWeek = 0;
     
      //确定1月1日是星期几
      COleDateTime tmStart(wYear, 1, 1, 0, 0, 0);
      int iFristWeekDay = tmStart.GetDayOfWeek(); //Sunday=1
      int iOffsetDay = iFristWeekDay;
      if(bMondayAsFirstDay) //周日为第一天
      {
        iOffsetDay = (iFristWeekDay-1)%7;
      }
      if(iOffsetDay==0) { iOffsetDay = 7; }
     
      COleDateTime tmCal(wYear, wMonth, wDay, 0, 0, 0);
      int iTotalDays = tmCal.GetDayOfYear() - 1; //January 1 = 1
     
      iWeek = (iTotalDays + iOffsetDay + 6)/7;
      ASSERT(iWeek >= 1 && iWeek <= 53);
     
      TRACE(_T("%04d-%02d-%02d (%d) WeekofYear=%d\n"),
        wYear, wMonth, wDay, bMondayAsFirstDay, iWeek);
     
      return iWeek;
    }

//测试代码

      GetWeeksOfYear(2015, 1, 1, 0);
      GetWeeksOfYear(2015, 1, 1, 1);
      GetWeeksOfYear(2015, 1, 3, 0);
      GetWeeksOfYear(2015, 1, 3, 1);
      GetWeeksOfYear(2015, 1, 4, 0);
      GetWeeksOfYear(2015, 1, 4, 1);
      GetWeeksOfYear(2015, 1, 5, 0);
      GetWeeksOfYear(2015, 1, 5, 1);
     
      GetWeeksOfYear(2016, 1, 1, 0);
      GetWeeksOfYear(2016, 1, 1, 1);
      GetWeeksOfYear(2016, 1, 2, 0);
      GetWeeksOfYear(2016, 1, 2, 1);
      GetWeeksOfYear(2016, 1, 3, 0);
      GetWeeksOfYear(2016, 1, 3, 1);
      GetWeeksOfYear(2016, 1, 4, 0);
      GetWeeksOfYear(2016, 1, 4, 1);
      
      GetWeeksOfYear(2017, 1, 1, 0);
      GetWeeksOfYear(2017, 1, 1, 1);
      GetWeeksOfYear(2017, 1, 2, 0);
      GetWeeksOfYear(2017, 1, 2, 1);
      GetWeeksOfYear(2017, 1, 7, 0);
      GetWeeksOfYear(2017, 1, 7, 1);
      GetWeeksOfYear(2017, 1, 8, 0);
      GetWeeksOfYear(2017, 1, 8, 1);
      GetWeeksOfYear(2017, 1, 9, 0);
      GetWeeksOfYear(2017, 1, 9, 1);
     
      GetWeeksOfYear(2018, 1, 1, 0);
      GetWeeksOfYear(2018, 1, 1, 1);
      GetWeeksOfYear(2018, 1, 6, 0);
      GetWeeksOfYear(2018, 1, 6, 1);
      GetWeeksOfYear(2018, 1, 7, 0);
      GetWeeksOfYear(2018, 1, 7, 1);
      GetWeeksOfYear(2018, 1, 8, 0);
      GetWeeksOfYear(2018, 1, 8, 1);


//输出

    2015-01-01 (0) WeekofYear=1
    2015-01-01 (1) WeekofYear=1
    2015-01-03 (0) WeekofYear=1
    2015-01-03 (1) WeekofYear=1
    2015-01-04 (0) WeekofYear=2
    2015-01-04 (1) WeekofYear=1
    2015-01-05 (0) WeekofYear=2
    2015-01-05 (1) WeekofYear=2
    2016-01-01 (0) WeekofYear=1
    2016-01-01 (1) WeekofYear=1
    2016-01-02 (0) WeekofYear=1
    2016-01-02 (1) WeekofYear=1
    2016-01-03 (0) WeekofYear=2
    2016-01-03 (1) WeekofYear=1
    2016-01-04 (0) WeekofYear=2
    2016-01-04 (1) WeekofYear=2
    2017-01-01 (0) WeekofYear=1
    2017-01-01 (1) WeekofYear=1
    2017-01-02 (0) WeekofYear=1
    2017-01-02 (1) WeekofYear=2
    2017-01-07 (0) WeekofYear=1
    2017-01-07 (1) WeekofYear=2
    2017-01-08 (0) WeekofYear=2
    2017-01-08 (1) WeekofYear=2
    2017-01-09 (0) WeekofYear=2
    2017-01-09 (1) WeekofYear=3
    2018-01-01 (0) WeekofYear=1
    2018-01-01 (1) WeekofYear=1
    2018-01-06 (0) WeekofYear=1
    2018-01-06 (1) WeekofYear=1
    2018-01-07 (0) WeekofYear=2
    2018-01-07 (1) WeekofYear=1
    2018-01-08 (0) WeekofYear=2
    2018-01-08 (1) WeekofYear=2
返回列表