Output the day of the week in a certain month and year

Output the day of the week in a certain month and year

topic

Insert picture description here

Code

Call it in the main function

int week= get_week(2019, 12, 4);
printf("2019年12月04日是星期%d\n",week);

Key core code

int get_week(int year,int month,int day){
    int days=0;
    int months[]={31,28,31,30,31,30,31,31,30,31,30,31};
    int i;
    // 累加年的天数
    for (i=1; i<year; i++) {
        if ((i%400==0)||(i%4==0&&i%100!=0)) {
            days+=366;  // 闰年366天,二月是29天
        }else{
            days+=365;
        }
    }
    for ( i=1; i<month; i++) {
        days+=months[i-1];
    }
    if (month>2&&(year%400==0||(year%4==0&&year%100!=100))) {
        days=days+1;  // 如果当前输入的年份是闰年并且月份比2大,加一
    }
    days = days+day;
    return days%7;
}

effect

Insert picture description here

Guess you like

Origin blog.csdn.net/honeylife/article/details/103394171