
#include<stadioh>
main()
{ int year,year1,month,month1,day,day1,age;
printf("请输入您的生日:");
scanf("%d %d %d",&year,&month,&day);
printf("请输入当前日期:");
scanf("%d %d %d",&year1,&month1,&day1);
if(year1==year)
age=0;
else
{age=year1-year;
if(month1<month||(month1==month&&day1<day))
age=age-1;
}
printf("您的年龄是:%d",age);
}
大概就是这个样子,我没有调试,可能有错误,但应该都是小问题,你自己调试一下,觉得哪儿不妥,可以自己改动。
#include <timeh>
#include <stdioh>
void main( void )
{
time_t ltime;
time( <ime );
printf( "The time is %s\n", ctime( <ime ) );
}
Output
The time is Sat May 26 10:25:12 2012
给你个更强大的程序,只要你输入那年的年和月
就能得出一分月历,不过是C++编的。
你的程序要求能用下边的部分代码,自己选择,
再加以完善,就能实现了。
#include<iostream>
#include<iomanip>
using namespace std;
//---------------------------------------------------------
void main()
{
int year1=1,month,wkday11=1,daysfrom11=0,year;
//year month --年 月;
//year1是计算的最初基数,也是能够查询到的最早年限;
//daysfrom11 -- 某年某月距离1月1日即元旦的天数,初值为0;
//功能实现靠daysfrom11累加而得;
int mthdays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//mthdays[i]表示月份为i的那一个月的天数(若闰年的话,2月分要再增加1天);
cout<<"请出入你所要查询的年份以及月份(例如2007 2):";
cin>>year>>month;//输入要查询的年份以及月份
int i,ans;
//----------------------------------------------------------
for(i=1;i<year;i++)
{
if(year==1)
break;
daysfrom11+=365;
if( (i%4==0)&&(i%100!=0)||(i%400==0) )
daysfrom11+=1; //从公元元年起到要查的年份的头一年总共有的天数(闰年加1);
}
//--------------------------------------------------------
for(i=1;i<month;i++)
daysfrom11+=mthdays[i];
if(month>2)
if( (year1%4==0)&&(year1%100!=0)||(year1%400==0) )
daysfrom11+=1; //从公元元年起到要查的年份以及月份总共有的天数((闰年2月加1));
ans=(wkday11+daysfrom11)%7; //ans用来决定日期是周几;
//-------------------------------------------------------------
cout<<year<<""<<month<<"月历表:"<<endl;
cout<<" "<<endl;
cout<<" Sun Mon Tue Wed Thu Fri Sat"<<endl;
cout<<" "<<endl;
//----------------------------------------------------------
int wday=ans;
cout<<" "<<setw(4(wday+1))<<1; //以4字节为单位表示输出
for(i=2;i<=mthdays[month];i++)
{
wday++;
if(wday%7==0)
{
cout<<endl;
cout<<" ";
wday=0;
}
cout<<setw(4)<<i; //以4字节为单位表示输出
}
cout<<endl;
cout<<" "<<endl;
}//======================================================
#include <timeh>
time_t time( time_t ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。可以调用ctime()函数进行时间转换输出:
char ctime(const time_t timer);
将日历时间转换成本地时间,按年月日格式,进行输出,如:
Wed Sep 23 08:43:03 2015
C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm gmtime(const time_t timer); //将日历时间转化为世界标准时间(即格林尼治时间)
struct tm localtime(const time_t timer); //将日历时间转化为本地时间
将通过time()函数返回的值,转换成时间结构struct tm :
struct tm {
int tm_sec; / 秒 – 取值区间为[0,59] /
int tm_min; / 分 - 取值区间为[0,59] /
int tm_hour; / 时 - 取值区间为[0,23] /
int tm_mday; / 一个月中的日期 - 取值区间为[1,31] /
int tm_mon; / 月份(从一月开始,0代表一月) - 取值区间为[0,11] /
int tm_year; / 年份,其值等于实际年份减去1900 /
int tm_wday; / 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /
int tm_yday; / 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /
int tm_isdst; / 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。/
};
编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。
例如:
#include<timeh>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/
}
C语言中读取系统时间的函数为time(),其函数原型为:
#include <timeh>
time_t time( time_t ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。可以调用ctime()函数进行时间转换输出:
char ctime(const time_t timer);
将日历时间转换成本地时间,按年月日格式,进行输出,如:
Wed Sep 23 08:43:03 2015
C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm gmtime(const time_t timer); //将日历时间转化为世界标准时间(即格林尼治时间)
struct tm localtime(const time_t timer); //将日历时间转化为本地时间
将通过time()函数返回的值,转换成时间结构struct tm :
struct tm {
int tm_sec; / 秒 – 取值区间为[0,59] /
int tm_min; / 分 - 取值区间为[0,59] /
int tm_hour; / 时 - 取值区间为[0,23] /
int tm_mday; / 一个月中的日期 - 取值区间为[1,31] /
int tm_mon; / 月份(从一月开始,0代表一月) - 取值区间为[0,11] /
int tm_year; / 年份,其值等于实际年份减去1900 /
int tm_wday; / 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 /
int tm_yday; / 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 /
int tm_isdst; / 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。/
};
编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。
例如:
#include<timeh>
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/
}
以上就是关于c语言编写程序 获取当前日期并计算X天后的年月日。全部的内容,包括:c语言编写程序 获取当前日期并计算X天后的年月日。、c语言中如何从网络获得当前日期(接近北京时间就行)、日期计算(C语言)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)