此函数的实现功能为计算自定义日期距离 2016年1月1日 的 天数 ,周数 ,星期 。
因为年份在定义的时候定义为 unsigned char 类型,所以有最大年份的限制,如果不想有限制,那么更改年份的定义就可以了。
//计算今天是今年的第几周星期几
//今天的日期自己定
#include<stdio.h>
//以下是计算的时间99年12月31日,这个时间可以改
#define SYSYEAR 99
#define SYSMONTH 12
#define SYSDAY 31
//结构体的定义部分
//系统时间
struct _SystemTime
{
unsigned char month; //系统月
unsigned char day; //系统天
unsigned char week; //系统星期
unsigned char year; //系统年
}SystemTime;
//判断是否是闰年
//返回1:是闰年
//返回0:不是闰年
char LeapYear()
{
unsigned char sysyear;
sysyear = SystemTime.year+2000;
/*
printf("=================判断是不是闰年===================\n");
printf("系统时间:\n");
printf("年:%d\n",SystemTime->year);
printf("月:%d\n",SystemTime->month);
printf("日:%d\n",SystemTime->day);
*/
if ( ( (sysyear%100!=0) && (sysyear%4==0) ) || (sysyear%400==0) )
{
//printf("今年是闰年\n");
return 1; //是闰年
}
else
{
//printf("今年不是闰年\n");
return 0; //不是闰年
}
}
//判断今天是今年的第几天
int numberday()
{
unsigned char i = 0;
unsigned short number = 0;
unsigned char monthnum[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
printf("=============判断今天是今年的第几天===============\n");
printf("系统时间:\n");
printf("年:%d\n",SystemTime.year);
printf("月:%d\n",SystemTime.month);
printf("日:%d\n",SystemTime.day);
*/
if(LeapYear()==1)
{
monthnum[1] = 29;
}
else
{
monthnum[1] = 28;
}
if(SystemTime.month != 1)
{
for(i;i<(SystemTime.month-1);i++)
{
number = number + monthnum[i];
//printf("整月天数%d\n",number);
}
number = number + SystemTime.day;
printf("今年天数%d\n",number);
}
else
{
number = SystemTime.day;
printf("今年天数%d\n",number);
}
return number;
}
//计算今天是今年的第几周
int countweek()
{
//2016年1月1日星期5
unsigned char year = 16;
unsigned char month = 1;
unsigned char day = 1;
unsigned char week = 5;
unsigned short yearstar;
unsigned char yuncount = 0;
unsigned char weeknumber = 0;
unsigned short totalday = 0;
unsigned short totalweek = 0;
/*
printf("=============算今天是今年的第几周===============\n");
printf("系统时间:\n");
printf("年:%d\n",SystemTime->year);
printf("月:%d\n",SystemTime->month);
printf("日:%d\n",SystemTime->day);
*/
/*
yearcha = SystemTime.year - year;
//printf("相差年数:%d\n",yearcha);
yuncount = yearcha / 4 + 1;
//printf("闰年个数:%d\n",yuncount);
*/
yearstar = 2000 + year;
for(yearstar;yearstar<(SystemTime.year+2000);yearstar++)
{
if ( ( (yearstar%100!=0) && (yearstar%4==0) ) || (yearstar%400==0) )
{
yuncount++;
//printf("闰年个数为:%d\n",yuncount);
}
}
printf("闰年个数为:%d\n",yuncount);
totalday = numberday();
//printf("当年天数:%d\n",totalday);
totalday = totalday + (SystemTime.year - year) * 365 + yuncount;
printf("一共天数:%d\n",totalday);
totalday = totalday - 1; //比如第二天说明过了一天,所以需要处理
weeknumber = totalday % 7 + week;
weeknumber = weeknumber % 7; //0表示星期日
//printf("星期余数:%d\n",weeknumber);
if(weeknumber == 0)
{
weeknumber = 7;
}
printf("===============================\n");
printf("\n");
printf("星期%d\n",weeknumber);
printf("\n");
printf("===============================\n");
if(weeknumber>=week)
{
return totalday/7+1;
}
else
{
return totalday/7+2;
}
}
unsigned short function()
{
unsigned short weekcount;
/*
printf("系统时间:\n");
printf("年:%d\n",SystemTime.year);
printf("月:%d\n",SystemTime.month);
printf("日:%d\n",SystemTime.day);
*/
weekcount = countweek( &SystemTime );
return weekcount;
}
int main()
{
unsigned short weekcount;
SystemTime.year = SYSYEAR;
SystemTime.month = SYSMONTH;
SystemTime.day = SYSDAY;
weekcount = function();
printf("\n\n");
printf("===============================\n");
printf("\n");
printf("当前是第%d周\n",weekcount);
printf("\n");
printf("===============================\n");
return 0;
}
//结果展示部分
/*
闰年个数为:21
今年天数365
一共天数:30681
===============================
星期4
===============================
===============================
当前是第4384周
===============================
*/