网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

c语言中日期的加减函数 c语言中日期的加减函数怎么算

求C语言程序,我输入日期(年月日),然后输入前后加减多少天,得出那个日期是多少

#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();

}

c语言中时间处理

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,这一对由于从面向对象哪里得到了赞助,用以写出的代码更觉优雅

求一段 日期时间类(时间加减转换) 代码的设计思路,有源码更好(源码语言 类C语言为好).

这是一个输入输出相互转换的题型,年月日与让天数相互转换。

天数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开始判断月份,

。。。。。。

c语言如何计算两个时间相差多少

/**

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语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。

时间加减 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类对时间进行处理,省去不必要的麻烦!

c语言时间函数的具体使用方法,时间的加减

#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


本文标题:c语言中日期的加减函数 c语言中日期的加减函数怎么算
网页路径:http://bjjierui.cn/article/hhjigs.html

其他资讯