User Tools

Site Tools

blog:2021-03-01_qt_getweeknumber_in_iso_format_start_from_sunday



2021-03-01 Qt: GetWeekNumber in ISO format start from Sunday

  • 使用Qt的 WeekNumber總是由Monday開始起算,無法獲得所要的
  • 因此自己做了一個
  • 這個Code所獲得之Week Number可與calendarWidget之Week Number相對應

Code

  • int GetWeekNumber(int year, int month, int day)
    {
        // 週數計算
        int lval_tempDayofYear = 0;
        int lvar_tempWeekNumber = 0;
        int lvar_ShiftDayOfWeek = QDate(year,1,1).dayOfWeek(); // 1月1日之星期幾
        int lval_AddWeekNumber = 0;
        if (lvar_ShiftDayOfWeek < 4) // 1月1日在周四之前
        {
            // 第一周在1月1日,否則在前一年之最後一周
            lval_AddWeekNumber = 1;
        }
        else
        {
            lval_AddWeekNumber = 0;
        }
        lval_tempDayofYear = QDate(year,month,day).dayOfYear();
        lvar_tempWeekNumber = ((lval_tempDayofYear + lvar_ShiftDayOfWeek - 1) / 7) + lval_AddWeekNumber;
        if (lvar_tempWeekNumber == 0)
        {
            return(GetWeekNumber(year-1, 12, 31));
        }
        return (lvar_tempWeekNumber);
    }

Reference

TAGS

  • 1165 person(s) visited this page until now.

Permalink blog/2021-03-01_qt_getweeknumber_in_iso_format_start_from_sunday.txt · Last modified: 2021/03/01 16:23 by jethro

oeffentlich