重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
#includestdio.h
在天橋等地區,都構建了全面的區域性戰略布局,加強發展的系統性、市場前瞻性、產品創新能力,以專注、極致的服務理念,為客戶提供成都網站建設、成都網站制作 網站設計制作專業公司,公司網站建設,企業網站建設,成都品牌網站建設,成都營銷網站建設,外貿網站建設,天橋網站建設費用合理。
#includeconio.h
#includestdlib.h
main()
{
int year,month,day;
int leapyear=0;//閏年時使用
int daytime=0;
int sum;
static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("請輸入日期:(例如:2010 5 27)");
scanf("%d%d%d",year,month,day);
if(year=0||month12||month1)//判斷輸入是否正確
{
printf("Error!\n");
getch();
exit(0);
}
if(year%400==0||(year%4==0year%100!=0))//判斷是否為閏年
leapyear=1;//是就加1
if(month==2)//判斷日是否輸入正確,2月是特殊的因為分閏年和平年
{ if((month_date[month-1]+leapyear)day||day=0)
{
printf("Error!\n");
getch();
exit(0);
}
}
if(month!=2)//當輸入不是2月時,判斷輸入
{
if(month_date[month-1]day||day=0)
{
printf("Error!\n");
getch();
exit(0);
}
}
printf("\n請輸入天數:");
scanf("%d",daytime);//輸入第N天后
if(daytime0)
{
printf("Error!\n");
getch();
exit(0);
}
sum=daytime+day;//當前日期與N天相加
do
{
if(month==2)//判斷當月是不是二月份
month_date[month-1]+=leapyear;
if(summonth_date[month-1])
{
sum-=month_date[month-1];
month++;//超過當月天數,加一個月
if(month==13)//當月份超過12月時,重新返到1月
{
year++;//加一年
if(year%400==0||(year%4==0year%100!=0))//判斷加一年后是不是閏年
leapyear=1;
else
leapyear=0;//不是閏年則為0
month=1;//因為12月的下一個月是1月
}
}
}while(summonth_date[month-1]);//當加起來的天數少于當月的天數就停止循環
day=sum;
printf("\n第%d天后是%d %d %d",daytime,year,month,day);//輸出
getch();
}
1. ctime函數
函數: ctime
功 能: 把日期和時間轉換為字符串
用 法: char *ctime(const time_t *time);
程序例:
#includecstdio
#includectime
intmain(void)
{
time_tt;
t=time(t);
printf("Today'sdateandtime:%s\n",ctime(t));
return0;
}
注:若在linux下使用本函數,需要include time.h頭文件
2.CTime類
CTime類的對象表示的時間是基于格林威治標準時間(GMT)的。CTimeSpan類的對象表示的是時間間隔。
CTime類一般不會被繼承使用。其對象的大小是8個字節。
CTime表示的日期上限是2038年1月18日,下限是1970年1月1日 12:00:00 AM GMT。
CTime類的成員函數
CTime();
構造一個未經初始化的CTime對象。此構造函數使我們可以定義一個CTime對象的數組,在使用數組前需要以有效的時間值為其初始化。
CTime(__time64_t time);
以一個__time64_t(注意:最前面的下劃線有兩條)類型的數據來構造一個CTime對象。參數time是一個__time64_t類型的值,表示自GMT時間1970年1月1日零點以來的秒數,這里要注意的是,參數time代表的時間會轉換為本地時間保存到構造的CTime對象中。例如,我們傳遞參數0構造一個CTime對象,然后調用CTime對象的GetHour成員函數將返回8,因為參數0代表的GMT時間轉換為北京時間后為1970年1月1日 8:00:00。
CTime(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
int nDST = -1
);
以本地時間的年、月、日、小時、分鐘、秒等幾個時間分量構造CTime對象。參數nYear、nMonth、nDay、nHour、nMin、nSec分別表示年、月、日、小時、分鐘、秒,取值范圍如下:
時間分量 取值范圍
nYear 1970-3000
nMonth 1-12
nDay 1-31
nHour 0-23
nMin 0-59
nSec 0-59
參數nDST指定是否實行夏令時,為0時表示實行標準時間,為正數時表示實行夏令時,為負數時由系統自動計算實行的是標準時間還是夏令時。
CTime(const SYSTEMTIME st,int nDST = - 1) ;
以一個SYSTEMTIME結構體變量來構造CTime對象。SYSTEMTIME結構體也是我們對日期時間的常用表示方式。參數st為以本地時間表示的SYSTEMTIME對象,參數nDST同上。
static CTime WINAPI GetCurrentTime( );
獲取系統當前日期和時間。返回表示當前日期和時間的CTime對象。
int GetYear( ) const;
獲取CTime對象表示時間的年份。范圍從1970年1月1日到2038年(包括2038年)1月18日。
int GetMonth( ) const;
獲取CTime對象表示時間的月份。范圍為1到12。
int GetDay( ) const;
獲取CTime對象表示時間的日期。范圍為1到31。
int GetHour( ) const;
獲取CTime對象表示時間的小時。范圍為0到23。
int GetMinute( ) const;
獲取CTime對象表示時間的分鐘。范圍為0到59。
int GetSecond( ) const;
獲取CTime對象表示時間的秒。范圍為0到59。
int GetDayOfWeek( ) const;
此函數的返回值表示CTime對象代表的是星期幾,1表示是周日,2表示是周一,以此類推。
CString Format(LPCTSTR pszFormat) const;
將CTime對象中的時間信息格式化為字符串。參數pszFormat是格式化字符串,與printf中的格式化字符串類似,格式化字符串中帶有%前綴的格式碼將會被相應的CTime時間分量代替,而其他字符會原封不動的拷貝到返回字符串中。格式碼及含義如下:
%a:周的英文縮寫形式。
%A:周的英文全名形式。
%b: 月的英文縮寫形式。
%B:月的英文全名形式。
%c: 完整的日期和時間。
%d:十進制形式的日期(01-31)。
%H:24小時制的小時(00-23)。
%I: 12小時制的小時(00-11)。
%j: 十進制表示的一年中的第幾天(001-366)。
%m: 月的十進制表示(01-12)。
%M:十進制表示的分鐘(00-59)。
%p: 12小時制的上下午標示(AM/PM)。
%S: 十進制表示的秒(00-59)。
%U: 一年中的第幾個星期(00-51),星期日是一周的第一天。
%W: 一年中的第幾個星期(00-51),星期一是一周的第一天。
%w: 十進制表示的星期幾(0-6)。
%Y: 十進制表示的年。
CTime operator +(CTimeSpan timeSpan) const;
將CTime對象和CTimeSpan對象相加,返回一個CTime對象。實際意義就是在一個時間的基礎上推后一個時間間隔,得到一個新的時間。
CTime operator -(CTimeSpan timeSpan) const;
將CTime對象和一個CTimeSpan相減,返回一個CTime對象。實際意義就是在一個時間的基礎上提前一個時間間隔,得到一個新的時間。
CTimeSpan operator -(CTime time) const;
將該CTime對象和另一個CTime對象相減,返回一個CTimeSpan對象。實際意義就是計算兩個時間點的間隔,得到一個CTimeSpan對象。
CTime operator +=(CTimeSpan span);
為該CTime對象增加一個span表示的時間間隔。
CTime operator -=(CTimeSpan span);
為該CTime對象減去一個span表示的時間間隔。
CTime operator =(__time64_t time);
為該CTime對象賦予一個新的時間值。
簡單說下剩下的幾個重載i運算符:
operator == : 比較兩個絕對時間是否相等。
operator != : 比較兩個絕對時間是否不相等。
operator : 比較兩個絕對時間,是否前一個大于后一個。
operator : 比較兩個絕對時間,是否前一個小于后一個。
operator = : 比較兩個絕對時間,是否前一個大于等于后一個。
operator = : 比較兩個絕對時間,是否前一個小于等于后一個。[1]
=====================================================================
C++中,CTime 與 CString轉換
CTime m_StartTime1 = CTime::GetCurrentTime();
CString csStartTime = m_StartTime1.Format( "%Y%m%d%H%M%S" );
一.將CString轉為CTime的幾種方法
CString timestr = "2000年04月05日";
int a,b,c ;
sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",a,b,c);
CTime time(a,b,c,0,0,0);
--------or - ---------------------
CString s("2001-8-29 19:06:23");
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", nYear, nMonth, nDate, nHour, nMin, nSec);
CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
---- or ------------------------
CString timestr = "2000年04月05日";
int year,month,day;
BYTE tt[5];
//get year
memset(tt, 0, sizeof(tt));
tt[0] = timestr[0];
tt[1] = timestr[1];
tt[2] = timestr[2];
tt[3] = timestr[3];
year= atoi((char *)tt);
//get month
memset(tt, 0, sizeof(tt));
tt[0] = timestr[6];
tt[1] = timestr[7];
month = atoi((char *)tt);
//get day
memset(tt, 0, sizeof(tt));
tt[0] = timestr[10];
tt[1] = timestr[11];
day = atoi((char *)tt);
CTime time(year,month,day,0,0,0);
從上面來看,很明顯使用sscanf()函數的優勢.
二.將CTIme轉換為CString的方法:
CTime tmSCan = CTime::GetCurrentTime();
CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'");
這樣得到的日期時間字符串就是以"2006-11-27 23:30:59"的格式.這是不是很方便呢?
//取得CTime中的日期
CString cstrDate = tmScan.Format("%Y-%m-%d");
//取得CTime中的時間
CString cstrTime = tmScan.Format("%H:%M-%S");
sprintf還有個不錯的表妹:strftime,專門用于格式化時間字符串的,用法跟她表哥很像,也是一大堆格式控制符,只是畢竟小姑娘家心細,她還要調用者指定緩沖區的最大長度,可能是為了在出現問題時可以推卸責任吧。這里舉個例子:
更多更好的sprintf()函數說明參考:《spirntf,你知道多少?》
time_t t = time(0);
//產生"YYYY-MM-DD hh:mm:ss"格式的字符串。
char s[32];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(t));
sprintf在MFC中也能找到他的知音:CString::Format,strftime在MFC中自然也有她的同道:CTime::Format,這一對由于從面向對象哪里得到了贊助,用以寫出的代碼更覺優雅
這是一個輸入輸出相互轉換的題型,年月日與讓天數相互轉換。
天數1.5為1900年1月1日 12:00時,1900年+1.5天
42576.85545表示2016/7/25 20:31.,用42576.85545除以365天約為116.648919041 +1900
可以這樣1900年加數字等于現在的時間,
1, 把數字分開,整數是天數,小數是小時,
2,用整數,判斷年月日,
2.1 if(190xx潤年?整數=366)1900++;整數-=366;
2.2 不夠365開始判斷月份,
。。。。。。
/**
time.c
定義一個結構體實現兩個時間的加減
*/
#includestdio.h
#includestring.h
typedef struct
{
int seconds;
int minutes;
int hours;
}Time;
int checkTime(Time time);
void printTime(Time time);
void swap(Time *time1,Time *time2);//大的時間放在前面
Time subtract1(Time *first,Time *second);
Time subtract(Time *first,Time *second);//默認第一個時間比第二個大
int main()
{
Time time1;
Time time2;
Time time3;
char againch[5]="y";
while(strcmp(againch,"y")==0||strcmp(againch,"Y")==0)
{
int again=1;
while(again)
{
printf("輸入時間1:");
scanf("%d:%d:%d",time1.hours,time1.minutes,time1.seconds);
if(checkTime(time1))
{
printf("-----輸入時間格式錯誤!請重新輸入\n");
again=1;
}
else
again=0;
}
again=1;
while(again)
{
printf("輸入時間2:");
scanf("%d:%d:%d",time2.hours,time2.minutes,time2.seconds);
if(checkTime(time2))
{
printf("-----輸入時間格式錯誤!請重新輸入\n");
again=1;
}
else
again=0;
}
swap(time1,time2);
printf("? ? ? ? ? ?");
printTime(time1);
printf(" - ");
printTime(time2);
time3=subtract(time1,time2);
printf(" = ");
printTime(time3);
printf("\n");
printf("繼續[y/n]?:");
scanf("%s",againch);
}
return 0;
}
//檢查時間的格式
int checkTime(Time time)
{
//? ? printf("小時格式錯誤:%d\n",(time.hours=24||time.hours0));
//? ? printf("分鐘格式錯誤:%d\n",(time.minutes=60||time.minutes0));
//? ? printf("秒格式錯誤? :%d\n",(time.seconds=60||time.minutes0));
return ((time.hours24||time.hours0)||(time.minutes=60||time.minutes0)||(time.seconds=60||time.minutes0));
}
//輸出按個數輸出時間
void printTime(Time time)
{
printf("%d:%d:%d",time.hours,time.minutes,time.seconds);
}
//大的時間放到第一個變量,小的時間方法哦第二個變量
void swap(Time *time1,Time *time2)
{
//保證第一個時間永遠大于第二個時間
if(time2-hourstime1-hours)//如果有time
{
//交換兩個時間的小時
time2-hours^=time1-hours;
time1-hours^=time2-hours;
time2-hours^=time1-hours;
//交換兩個時間的分鐘:
time1-minutes^=time2-minutes;
time2-minutes^=time1-minutes;
time1-minutes^=time2-minutes;
//交換兩個時間的秒:
time1-seconds^=time2-seconds;
time2-seconds^=time1-seconds;
time1-seconds^=time2-seconds;
}
else if(time2-minutestime1-minutestime1-hours==time2-hours)
{
//交換兩個時間的分鐘:
time1-minutes^=time2-minutes;
time2-minutes^=time1-minutes;
time1-minutes^=time2-minutes;
//交換兩個時間的秒:
time1-seconds^=time2-seconds;
time2-seconds^=time1-seconds;
time1-seconds^=time2-seconds;
}
else if(time2-secondstime1-secondstime1-minutes==time2-minutes)
{
//交換兩個時間的秒:
time1-seconds^=time2-seconds;
time2-seconds^=time1-seconds;
time1-seconds^=time2-seconds;
}
}
//計算兩個時間的差
Time subtract(Time *first,Time *second)//默認第一個時間比第二個大
{
Time result;
//先對秒進行相減
if(first-seconds=second-seconds)//如果第一個秒大于或者等于
{
result.seconds=first-seconds-second-seconds;
}
else//如果第一個的秒數小的話
{
first-minutes=first-minutes-1;//借位
first-seconds=first-seconds+60;
result.seconds=first-seconds-second-seconds;
}
//接著對分鐘相減
if(first-minutes=second-minutes)//如果第一個秒大于或者等于
{
result.minutes=first-minutes-second-minutes;
}
else//如果第一個的秒數小的話
{
first-hours=first-hours-1;//借位
first-minutes=first-minutes+60;
result.minutes=first-minutes-second-minutes;
}
//交換后 默認第一個小時會大于第一個,沒有借位的情況,不用
result.hours=first-hours-second-hours;
return result;
拓展資料
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平臺的特性,以一個標準規格寫出的C語言程序可在許多電腦平臺上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平臺。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標準局為C語言制定了一套完整的美國國家標準語法,稱為ANSI C,作為C語言最初的標準。目前2011年12月8日,國際標準化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標準,也是C語言的最新標準,該標準更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。
在VC中我是這樣弄的:
SYSTEMTIME time;
::GetSystemTime(time);\\取當前時間,time為一個臨時的變量
CTime now=time;\\將當前時間存到CTime變量中
\\輸入要進行對比的時間存入time中,比如下面(對比時間是2006年8月20日):
time.wYear=2006;
time.wMonth=8;
time.wDay=20;
CTime cmptime=time;\\將要對比的時間放到另一個CTime變量中
CTimeSpan overtime=now-cmptime;\\比較時,兩時間直接作差,保存在CTimeSpan變量中,這個值有正負,overtime雖然是CTimeSpan類型的,但是用法和CTime一樣
int overdays=overtime.GetDays();\\取天數差距,本例中就為9(今天是8月29日),如果剛才now-cmptime換成cmptime-now,現在的值就是-9
int overdays=overtime.GetMonths();同上,取月差距,其他方法一樣,年,分,秒都能比較
自己回去試一試吧,應該有滿意的效果!
我也是琢磨了很長時間才弄出來,個人覺得算是比較方便的方法了,正如你所說,時間的計算比較復雜,容易出錯,不如讓提供好的MFC類對時間進行處理,省去不必要的麻煩!
#include?stdio.h
#include?time.h?
int?main()
{?
time_t?rawtime;?
struct?tm?*?timeinfo;?
time?(?rawtime?);?
timeinfo?=?localtime?(?rawtime?);?
printf?(?"The?current?date/time?is:?%s",?asctime?(timeinfo)?);?
return?0;
}
說明:
time_t // 時間類型(time.h 定義)?
struct tm { // 時間結構,time.h 定義如下:?
int tm_sec;?
int tm_min;?
int tm_hour;?
int tm_mday;?
int tm_mon;?
int tm_year;?
int tm_wday;?
int tm_yday;?
int tm_isdst;?
}?
time ( rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存于rawtime?
localtime ( rawtime ); //轉為當地時間,tm 時間結構?
asctime() // 轉為標準ASCII時間格式:?
//就是直接打印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1