用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。

用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。,第1张

#include<stdioh>

intmonthsize(intyear,intmonth){

intdays;

if(month==2){

if(year%4==0&&year%100!=0||year%400==0)

return29;

return28;

}

switch(month){

case1:

case3:

case5:

case7:

case8:

case10:

case12:days=31;break;

case4:

case6:

case9:

case11:days=30;break;

}

returndays;

}

intmain(){

intyear,month,day,days;

printf("年月日:");

while(scanf("%d%d%d",&year,&month,&day)==3){

days=monthsize(year,month);

if(days==day){

if(month==12){

++year;

month=1;

day=1;

}

else++month;

}

else++day;

printf("第二天是:%d/%02d/%02d\n",year,month,day);

printf("年月日(qtoquit):");

}

return0;

}

扩展资料

输入两个任意日期计算相隔天数

#include<stdioh>

#include<stdlibh>

#include<stringh>

#include<mathh>

#include<timeh>

intget_days(constcharfrom,constcharto);

time_tconvert(intyear,intmonth,intday);

intmain()

{

constcharfrom=“2013-3-15”;

constcharto=“2015-8-14”;

intdays=get_days(from,to);

printf(“From:%s\nTo:%s\n”,from,to);

printf(“%d\n”,天);

个系统(“暂停”);

返回0;

}

20time_tconvert(intyear,intmonth,intday)

{

tminfo={0};

infotm_year=year-1900;

infotm_mday=天;

返回mktime(&info);

}

intget_days(constcharfrom,constcharto)

{

int年,月,日;

sscanf(from,“%d-%d-%d”,&year,&month,&day);

intfromSecond=(int)convert(年,月,日);

sscanf(to,“%d-%d-%d”,&year,&month,&day);

inttoSecond=(int)convert(年,月,日);

return(toSecond-fromSecond)/24/3600;

}

#include <stdioh>

#include <timeh>

int main()

{

 time_t rawtime;

 struct tm timeinfo;

 time ( &rawtime );

 timeinfo = localtime ( &rawtime );

 int year,month,day,hour,min,sec;

 year = 1900+timeinfo->tm_year;

 month = 1+timeinfo->tm_mon;

 day = timeinfo->tm_mday;

 hour = timeinfo->tm_hour;

 min = timeinfo->tm_min;

 sec = timeinfo->tm_sec;

 printf ( "当前时间:%4d-%02d-%02d %02d:%02d:%02d\n\n",year, month,day,hour,min,sec);

 printf ( "你需要的格式:%4d%02d%02d%02d%02d%02d\n\n",year, month,day,hour,min,sec);

char hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];

 sprintf(hyy,"%02X",year/100);

 sprintf(lyy,"%02X",year%100);

 sprintf(MM,"%02X",month);

 sprintf(dd,"%02X",day);

 sprintf(hh,"%02X",hour);

 sprintf(mm,"%02X",min);

 sprintf(ss,"%02X",sec);

printf("转化为16进制:%02s%02s%02s%02s%02s%02s%02s\n",hyy,lyy,MM,dd,hh,mm,ss);

 //exit(0);

 return 0;

}

说明:我用VC60现写的,运行结果截图如下:

#include <stdioh>

#include <timeh>

int main()

{

time_t timep; //时间变量,从1970年1月1日0时起的秒数

struct tm p; //时间结构,含年月日时分秒星期几,一年中第几天,夏时制等成员。年从1900起算,月从0起算,

time(&timep); // 获取当前时间,从1970年1月1日0时起的秒数

p = gmtime(&timep); // 获取UTC时间 结构成员数值们

printf("%d %d %d\n",1900+p->tm_year, 1+p->tm_mon, p->tm_mday); //输出UTC时间的年月日

p = localtime(&timep); // 获取本地 时间 结构成员数值们

printf("%d %d %d\n",1900+p->tm_year, 1+p->tm_mon, p->tm_mday); //输出本地时间年月日

return 0;

}

方法一,#include<timeh>

int main()

{

time_t timep;

struct tm p;

time (&timep);

p=gmtime(&timep);

printf("%d\n",p->tm_sec); /获取当前秒/

printf("%d\n",p->tm_min); /获取当前分/

printf("%d\n",8+p->tm_hour);/获取当前时,这里获取西方的时间,刚好相差八个小时/

printf("%d\n",p->tm_mday);/获取当前月份日数,范围是1-31/

printf("%d\n",1+p->tm_mon);/获取当前月份,范围是0-11,所以要加1/

printf("%d\n",1900+p->tm_year);/获取当前年份,从1900开始,所以要加1900/

printf("%d\n",p->tm_yday); /从今年1月1日算起至今的天数,范围为0-365/

}

方法二#include <stdioh>

#include <timeh>

int main ()

{

time_t t

struct tm  lt;    time (&t);//获取Unix时间戳。

lt = localtime (&t);//转为时间结构。

printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday,

lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果

return 0;}

扩展资料

1、CTimeSpan类

如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:

CTime t1( 1999, 3, 19, 22, 15, 0 );

CTime t = CTime::GetCurrentTime();

CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔

int iDay=spanGetDays(); //获取这段时间间隔共有多少天

int iHour=spanGetTotalHours(); //获取总共有多少小时

int iMin=spanGetTotalMinutes();//获取总共有多少分钟

int iSec=spanGetTotalSeconds();//获取总共有多少秒

2、timeb()函数

_timeb定义在SYS\TIMEBH,有四个fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb timeptr );

struct _timeb timebuffer;

_ftime( &timebuffer );

参考资料来源:百度百科:time函数

以上就是关于用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。全部的内容,包括:用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。、用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)、C语言时间函数,求解析意思等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9543353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存