2014-09-20 58 views
0

此代碼是獲取當前的日期和dislpaying它使用日期和時間函數

#include stdio.h 
#include time.h 

int main(void) 

{ 

char currentt[80]; 

time_t now = time(NULL); 

struct tm *t = localtime(&now); 

strftime (currentt,80,"%d/%m/%Y",t+=30); 

puts (currentt); 

printf("%s",currentt); 

return 0; 

} 

,我必須手動輸入該加上30天另一個代碼日期

#include stdio.h 
#include time.h 

int main() 

{   

    /* initialize */ 
    int y=2014, m=9, d=19;  
    struct tm t = { .tm_year=y-1900, .tm_mon=m-1, .tm_mday=d }; 
    /* modify */ 
    t.tm_mday += 30; 
    mktime(&t); 
    /* show result */ 
    printf(asctime(&t)); 
return 0; 
} 

我想什麼do是合併這種代碼的方式,它從 FIRST代碼和ADD 30天使用第二代碼.... 任何人都可以幫助我這個。 任何其他的邏輯也將不勝感激,但我想用C語言。

+0

進行函數包含'第一code'或單一合併兩個'main'的代碼' main'。 – 2014-09-20 04:02:52

+0

是的,我知道我ned合併它,但不知道如何使用第一個代碼的結構,並通過當前系統日期秒,並添加30天... – 2014-09-20 04:15:59

回答

1

第一個#include應與<>圍繞文件名一起使用。下面的代碼與上面的兩個代碼類似。我已在適當的地方提出意見。它只是獲取當前時間加上30天的日子重新計算領域的一個新的時間和輸出在第二`code`

#include <stdio.h> 
#include <time.h> 

int main() 
{ 
    /* Get the current time*/ 
    time_t now = time(NULL); 
    struct tm *t = localtime(&now); 

    /* modify current time by adding 30 days*/ 
    t->tm_mday += 30; 
    mktime(t); 

    /* show result */ 
    printf(asctime(t)); 
    return 0; 
} 
+0

Thankyou太多了......它的工作原理! – 2014-09-20 04:27:29