日期函数操作模板
1 #include<iostream> 2 #include<ctime> 3 #include<cstring> 4 using namespace std; 5 const char weekdiary[][6]={ 6 {""}, 7 {"Mon"}, 8 {"Tue"}, 9 {"Wed"}, 10 {"Thur"}, 11 {"Fri"}, 12 {"Sat"}, 13 {"Sun"}, 14 }; 15 16 const char monthdiary[][6]={ 17 {""}, 18 {"Jan"}, 19 {"Feb"}, 20 {"Mar"}, 21 {"Apr"}, 22 {"May"}, 23 {"Jun"}, 24 {"Jul"}, 25 {"Aug"}, 26 {"Sept"}, 27 {"Oct"}, 28 {"Nov"}, 29 {"Dec"}, 30 }; 31 32 int main() 33 { 34 time_t now = time(0); 35 char* t = ctime(&now); 36 cout<<t; 37 // 38 char week[6]; 39 char month[6]; 40 memset(week,0,sizeof(week)); 41 memset(month,0,sizeof(month)); 42 int day,hour,minute,second,year; 43 sscanf(t,"%s %s %d %d:%d:%d %d\n",week,month,&day,&hour,&minute,&second,&year); 44 // 45 cout<<week<<endl; 46 cout<<month<<endl; 47 printf("---------------\n"); 48 for(int i=1;i<8;i++) 49 { 50 if(!memcmp(week,weekdiary[i],sizeof(weekdiary[i]))) 51 { 52 cout<<"weekday "<<i<<endl; 53 break; 54 } 55 } 56 for(int i=1;i<13;i++) 57 { 58 if(!memcmp(month,monthdiary[i],sizeof(monthdiary[i]))) 59 { 60 cout<<"month "<<i<<endl; 61 break; 62 } 63 } 64 printf("%d\n",day); 65 printf("%d\n",hour); 66 printf("%d\n",minute); 67 printf("%d\n",second); 68 printf("%d\n",year); 69 return 0; 70 }