C 语言 如何做时间的加法

ydb7459022 2012-01-19 03:21:04
我要求一个日期后面三天的日期,如何做加法/

例:当前时间2011/11/30 ,后面三天的时间如何计算,

即: 2011/11/30 +3 =2011/12/3

有什么函数吗?
在线等等…………
...全文
339 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-01-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhao4zhong1 的回复:]
C/C++ code
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
COleDateTime t;
COleDateTimeSpan ts;
CString s,fmt;
int nYear;
i……
[/Quote]
The COleDateTime class handles dates from 1 January 100 – 31 December 9999.
AnYidan 2012-01-20
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
/* day_of_year: set day of year from month & day */
int day_of_year(int year, int month, int day)
{
int i, leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for (i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}

/* month_day: set month, day from day of year */
void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for (i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
*pmonth = i;
*pday = yearday;
}
int main()
{
int m, d;
month_day(1988, 60, &m, &d);
printf("%d month %d date\n", m, d);
system("pause");
}

The c programming language 上的例题,可参照
JackBurd 2012-01-20
  • 打赏
  • 举报
回复
只需确定当月的天数(特殊处理二月,根据是非闰年确定天数)即可。进行可能的月份累加或年份累加。
JackBurd 2012-01-20
  • 打赏
  • 举报
回复
只需确定当月的天数(特殊处理二月,根据是非闰年确定天数)即可。
秃头披风侠 2012-01-20
  • 打赏
  • 举报
回复
这种加法直接用判断不就能做么...
顺便膜拜2楼....学习了...
ProgrammingRing 2012-01-20
  • 打赏
  • 举报
回复
make。。以后备用
mymtom 2012-01-19
  • 打赏
  • 举报
回复

/*-
* Copyright (C), 1988-2012, mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */

/**
* @file tm.c
* @brief
*/

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

#define HAVE_STRPTIME 1

int main(int argc, char *argv[])
{
time_t tick;
struct tm tm;
char buf[20];

memset(&tm, 0, sizeof(tm));
#if HAVE_STRPTIME
strptime("2011/11/30", "%Y/%m/%d", &tm);
#else
tm.tm_year = 2011 - 1900;
tm.tm_mon = 11 - 1;
tm.tm_mday = 30;
#endif

tm.tm_mday += 3;
tm.tm_isdst = -1;
tick = mktime(&tm);

tm = *localtime(&tick);
strftime(buf, sizeof(buf), "%Y/%m/%d", &tm);
printf("%s\n", buf);

return 0;
}
ybjx111 2012-01-19
  • 打赏
  • 举报
回复

tm t1;
time_t t2;
time(&t2);
localtime_s(&t1,&t2);
t.tm_sec+=20;
t2=mktime(t1);
localtime_s(&t1,&t2);
//取20秒后的时间,其他类似
IVERS0N 2012-01-19
  • 打赏
  • 举报
回复
time.h
LinkSe7en 2012-01-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ydb7459022 的回复:]

这个太多了吧,我想要个简单点的,
[/Quote]

这就叫多 这就叫复杂???我看你还是绕行吧
ydb7459022 2012-01-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhongguoren666 的回复:]

http://blog.csdn.net/barsdy/article/details/4029864
[/Quote]

你这里有加法吗??
ydb7459022 2012-01-19
  • 打赏
  • 举报
回复
这个太多了吧,我想要个简单点的,
zhongguoren666 2012-01-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/barsdy/article/details/4029864
赵4老师 2012-01-19
  • 打赏
  • 举报
回复
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
COleDateTime t;
COleDateTimeSpan ts;
CString s,fmt;
int nYear;
int nMonth;
int nDay;
int nHour;
int nMin;
int nSec;
int lDays;
int nHours;
int nMins;
int nSecs;
int i,N;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf("Fatal Error: MFC initialization failed\n");
return 1;
}
if (argc<13) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 2;
}
if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
else {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 3;
}
nYear =atoi(argv[ 1]);
nMonth=atoi(argv[ 2]);
nDay =atoi(argv[ 3]);
nHour =atoi(argv[ 4]);
nMin =atoi(argv[ 5]);
nSec =atoi(argv[ 6]);
lDays =atoi(argv[ 7]);
nHours=atoi(argv[ 8]);
nMins =atoi(argv[ 9]);
nSecs =atoi(argv[10]);
N =atoi(argv[11]);
if (N<=0) {
printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
return 4;
}
t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
for (i=1;i<=N;i++) {
s=t.Format(fmt);
printf("%08d %s\n",i,s);
t=t+ts;
}
return 0;
}
单片机为什么还在用C语言编程?答案是:C语言是最适合单片机编程的高级语言。这个问题的意思应该是:现在有很多很好用的高级语言,如java,python,VC等等,为什么这些语言不能用来编写单片机程序呢?那么这个问题的答案就是:不能不能,而是不合适。一、单片机编程的特点对单片机编程来说,首先要考虑的是单片机的程序空间和数据空间都是有限的,所以要让程序尽量短小精悍,以节省程序占用的存储空间。第二、单片机编程的一个主要对象是对单片机的端口和内部寄存器的操作和配置,这个需要比较精确的时序控制。第三、单片机算法运算中,尽量使用加法、减法、移位运算,因为乘法和除法运算会非常费时间,尤其是除法,会耗费很多时间,这对于速度本身就有限制的单片机来说,是一个很大的负担。二、高级语言编写单片机程序的缺陷高级语言可以实现更为优化的算法,更为方便的执行方案,但是,高级语言对程序存储空间的占用要比汇编和C语言多很多。这是最致命的一点,单片机有限的存储空间需要靠精打细算来设计程序,根本经不起高级语言臃肿的代码体积。高级语言无法实现精确的时序控制。三、C语言是一个折中选择其实用C语言开发单片机也是一个折中方案,因为最适

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧