且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何编码一年中的几周?

更新时间:2023-02-06 20:25:37

一年中既没有52周或53周。 365或366天相当于52周和1或2天。正如您在代码中包含time.h一样,您应该使用系统时间函数来获得答案。而你的问题并不清楚你想要实现的目标。您可以查看根据ISO8601的周数 [ ^ ]以获取更多信息。

这很复杂:没有一年中只有52或53周,也可能有51个。问题是 ISO 8601标准 [ ^ ]将第一周定义为星期一开始,年份的第一个星期四开始 - 所以1月1日可以是第一周或第二周,或者int上一年的51,52或53周......



这可能会有所帮助:提供周数的DateTime扩展方法 [ ^ ]它在C#中,buit代码非常明显。请记住,ISO标准周从星期一开始,而不是星期天!



还有另一个皱纹,在那个闰年意味着有两个不同的定义一年中的一天同样的:3月1日是第60天或第61天,取决于闰年,另一个是60天和2月29日是闰年的第366天(后者有时用于食品行业的生产日期到保持其他日期的数字相同)。


问题有点大,因为你必须尊重闰年。比你一年中的所有日子都可以开始计算工作日01.01。和31.12。每年最后得到几周。



我认为第一步是计算01.01的工作日。希望的一年,而不是从那个移动的目标获得一周。


I started the program on January 1, 1900. Each year there are 52 weeks or 53 weeks. I don't know how to calculate how many weeks. Help me! Thank you very much!

Example: Input 18-09-1999
-> Output: This is Saturday.
This is 261 days of the year.
This is week 38 of the year.

In fact, it wouuld be week 37.

What I have tried:

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
int main(int argv, char** arv) {
    int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char *day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    int d, m, y, i;
    int temp;
    printf("Fill in a date after 01-01-1900 as dd-mm-yyyy: ");
    scanf("%d-%d-%d",  &d, &m, &y);
    temp=d;
    // correction for leap year
    if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
        month[1] = 29;
    if (y < 1900 || m < 1 || m > 12 || d < 1 || d > month[m - 1]) {
        printf("This is an invalid date.\n");
        return 1;
    }
    for (i = 1900; i < y; i++){
        if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
            d += 366;
        else
            d += 365;
    }
    for (i = 0; i < m - 1; i++){
        d += month[i];
    }
    printf("This is %s.\n", day[d % 7]);
    // day of year.........................
    int DayofYear=temp;
    for(i=0;i<(m-1);i++)
    	DayofYear = Dayofyear +month[i];
    printf("This is %d day of the year\n", DayofYear);
    //week of year.
    printf("This is week %d of the year", (DayofYear/7)+1);
}

There are neither 52 or 53 weeks in a year. There are 365 or 366 days which equates to 52 weeks and 1 or 2 days. As you are including time.h in your code you should use the system time functions to get the answer. And your question is not clear about what you are trying to achieve. You may like to look at Week numbers according to ISO8601[^] for further information.


That's complicated: there aren't "just 52 or 53 weeks in the year", there can be 51 as well. The problem is that the ISO 8601 standard[^] defines week one as starting on the Monday of the week with the year's first Thursday in - So Jan 1st can be in week one or two, or int week 51, 52, or 53 of the previous year ...

This may help: DateTime Extension Method to Give Week Number[^] it's in C#, buit teh code s pretty obvious. Remember that ISO standard weeks start on Monday, not Sunday!

There is another wrinkle, in that leap years mean that there are two different definitions of "day of the year" as well: in one March 1st is day 60 or 61 depending on the leap year, in the other it's always day 60 and Feb 29th is day 366 during a leap year (The latter is sometimes used in the food industry for production dates to keep the other day numbers the same).


The problem is a bit bigger because you must respect leap years too. Than you have all days of a year and can start calculating the week day 01.01. and 31.12. of each year and finally get the weeks.

I think the first step is to calculate the weekday of 01.01. of the wanted year and than getting the week from that "moving target".