c/c++ 取两个日期之间的 日,时,分(邦定也给分)

LGame 2006-05-31 08:09:51
例:

有两个日期 是 char[24]的

分别是 20060326 15:36:14
20060327 10:10:00

现在想取出他们之间相差得

日,时,分

谢谢了!
...全文
527 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
LGame 2006-06-08
  • 打赏
  • 举报
回复
CTIME

还有哪个?
吃狼的豆腐 2006-06-04
  • 打赏
  • 举报
回复
标准c时间与日期函数

--------------------------------------------------------------------------------

asctime
语法:


#include <time.h>
char *asctime( const struct tm *ptr );


功能: 函数将ptr所指向的时间结构转换成下列字符串:

day month date hours:minutes:seconds year\n\0
例如:

Mon Jun 26 12:03:53 2000
相关主题:
localtime(), gmtime(), time(), and ctime().


--------------------------------------------------------------------------------

clock
语法:


#include <time.h>
clock_t clock( void );


功能:函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。 转换返回值以秒记, 返回值除以CLOCKS_PER_SECOND. (注: 如果编译器是POSIX兼容的, CLOCKS_PER_SECOND定义为1000000.)

相关主题:
time(), asctime(), and ctime().


--------------------------------------------------------------------------------

ctime
语法:


#include <time.h>
char *ctime( const time_t *time );


功能:函数转换参数time为本地时间格式:

day month date hours:minutes:seconds year\n\0
ctime() 等同

asctime( localtime( tp ) );
相关主题:
localtime(), gmtime(), time(), and asctime().


--------------------------------------------------------------------------------

difftime
语法:


#include <time.h>
double difftime( time_t time2, time_t time1 );


功能:函数返回时间参数time2和time1之差的秒数表示。

相关主题:
localtime(), gmtime(), time(), and asctime().


--------------------------------------------------------------------------------

gmtime
语法:


#include <time.h>
struct tm *gmtime( const time_t *time );


功能:函数返回给定的统一世界时间(通常是格林威治时间),如果系统不支持统一世界时间系统返回NULL。 警告!

相关主题:
localtime(), time(), and asctime().


--------------------------------------------------------------------------------

localtime
语法:


#include <time.h>
struct tm *localtime( const time_t *time );


功能:函数返回本地日历时间。警告!

相关主题:
gmtime(), time(), and asctime().


--------------------------------------------------------------------------------

mktime
语法:


#include <time.h>
time_t mktime( struct tm *time );


功能:函数转换参数time 类型的本地时间至日历时间,并返回结果。如果发生错误,返回-1。

相关主题:
time(), gmtime(), asctime(), and ctime().


--------------------------------------------------------------------------------

strftime
语法:


#include <time.h>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );


功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,然后存储在字符串str中(至多maxsize 个字符)。用于设定时间不同类型的代码为:

代码
含义

%a
星期的缩略形式

%A
星期的完整形式

%b
月份的缩略形式

%B
月份的完整形式

%c
月份的缩略形式

%d
月中的第几天(1-31)

%H
小时, 24小时格式 (0-23)

%I
小时, 12小时格式 (1-12)

%j
年中的第几天(1-366)

%m
月份 (1-12). Note: 某些版本的Microsoft Visual C++ 可能使用取值范围0-11.

%M
分钟(0-59)

%p
本地时间的上午或下午(AM or PM)

%S
秒钟(0-59)

%U
年中的第几周,星期天是一周的第一天

%w
星期几的数字表示(0-6, 星期天=0)

%W
一年中的第几周,星期天是一周的第一天

%x
标准日期字符串

%X
标准时间字符串

%y
年(0-99)

%Y
用CCYY表示的年(如:2004)

%Z
时区名

%%
百分号


函数strftime()返回值为处理结果字符串str中字符的个数,如果发生错误返回零。

相关主题:
time(), localtime(), and gmtime().


--------------------------------------------------------------------------------

time
语法:


#include <time.h>
time_t time( time_t *time );


功能: 函数返回当前时间,如果发生错误返回零。如果给定参数time ,那么当前时间存储到参数time中
limlzm 2006-06-03
  • 打赏
  • 举报
回复
是想做成uptime的功能吗?
0黄瓜0 2006-06-03
  • 打赏
  • 举报
回复
double a = 1.23232323232;
int b = (int)a;
=======================
你在哪里看到b是0?
我得到的b仍然是1嘛
LGame 2006-06-02
  • 打赏
  • 举报
回复
我这怎么只有strftime



你说有CTIME 还有个别的

别的是什么?

还一问题

double a = 1.23232323232;
int b = (int)a;

这时为什么b会是0

因为超位了?
ymx0330 2006-06-02
  • 打赏
  • 举报
回复
LZ调错厉害呀~~
呵呵
帮顶
YFY 2006-06-01
  • 打赏
  • 举报
回复
NAME
strptime - date and time conversion
SYNOPSIS


#include <time.h>

char *strptime(const char *buf, const char *format, struct tm *tm);


DESCRIPTION
The strptime() function converts the character string pointed to by buf to values which are stored in the tm structure pointed to by tm, using the format specified by format.
The format is composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters (as specified by isspace(); an ordinary character (neither % nor a white-space character); or a conversion specification. Each conversion specification is composed of a % character followed by a conversion character which specifies the replacement required. There must be white-space or other non-alphanumeric characters between any two conversion specifications. The following conversion specifications are supported:

%a
is the day of week, using the locale's weekday names; either the abbreviated or full name may be specified.
%A
is the same as %a.
%b
is the month, using the locale's month names; either the abbreviated or full name may be specified.
%B
is the same as %b.
%c
is replaced by the locale's appropriate date and time representation.
%C
is the century number [0,99]; leading zeros are permitted but not required.
%d
is the day of month [1,31]; leading zeros are permitted but not required.
%D
is the date as %m/%d/%y.
%e
is the same as %d.
%h
is the same as %b.
%H
is the hour (24-hour clock) [0,23]; leading zeros are permitted but not required.
%I
is the hour (12-hour clock) [1,12]; leading zeros are permitted but not required.
%j
is the day number of the year [1,366]; leading zeros are permitted but not required.
%m
is the month number [1,12]; leading zeros are permitted but not required.
%M
is the minute [0-59]; leading zeros are permitted but not required.
%n
is any white space.
%p
is the locale's equivalent of a.m or p.m.
%r
12-hour clock time using the AM/PM notation if t_fmt_ampm is not an empty string in the LC_TIME portion of the current locale; in the POSIX locale, this will be equivalent to %I:%M:%S %p.
%R
is the time as %H:%M.
%S
is the seconds [0,61]; leading zeros are permitted but not required.
%t
is any white space.
%T
is the time as %H:%M:%S.
%U
is the week number of the year (Sunday as the first day of the week) as a decimal number [00,53]; leading zeros are permitted but not required.
%w
is the weekday as a decimal number [0,6], with 0 representing Sunday; leading zeros are permitted but not required.
%W
is the the week number of the year (Monday as the first day of the week) as a decimal number [00,53]; leading zeros are permitted but not required.
%x
is the date, using the locale's date format.
%X
is the time, using the locale's time format.
%y
is the year within century. When a century is not otherwise specified, values in the range 69-99 refer to years in the twentieth century (1969 to 1999 inclusive); values in the range 00-68 refer to years in the twenty-first century (2000 to 2068 inclusive). Leading zeros are permitted but not required.
%Y
is the year, including the century (for example, 1988).
%%
is replaced by %.
Modified Directives
Some directives can be modified by the E and O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified directive. If the alternative format or specification does not exist in the current locale, the behaviour will be as if the unmodified directive were used.
%Ec
is the locale's alternative appropriate date and time representation.
%EC
is the name of the base year (period) in the locale's alternative representation.
%Ex
is the locale's alternative date representation.
%EX
is the locale's alternative time representation.
%Ey
is the offset from %EC (year only) in the locale's alternative representation.
%EY
is the full alternative year representation.
%Od
is the day of the month using the locale's alternative numeric symbols; leading zeros are permitted but not required.
%Oe
is the same as %Od.
%OH
is the hour (24-hour clock) using the locale's alternative numeric symbols.
%OI
is the hour (12-hour clock) using the locale's alternative numeric symbols.
%Om
is the month using the locale's alternative numeric symbols.
%OM
is the minutes using the locale's alternative numeric symbols.
%OS
is the seconds using the locale's alternative numeric symbols.
%OU
is the week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols.
%Ow
is the number of the weekday (Sunday=0) using the locale's alternative numeric symbols.
%OW
is the week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.
%Oy
is the year (offset from %C) using the locale's alternative numeric symbols.
A directive composed of white-space characters is executed by scanning input up to the first character that is not white-space (which remains unscanned), or until no more characters can be scanned.

A directive that is an ordinary character is executed by scanning the next character from the buffer. If the character scanned from the buffer differs from the one comprising the directive, the directive fails, and the differing and subsequent characters remain unscanned.

A series of directives composed of %n, %t, white-space characters or any combination is executed by scanning up to the first character that is not white space (which remains unscanned), or until no more characters can be scanned.

Any other conversion specification is executed by scanning characters until a character matching the next directive is scanned, or until no more characters can be scanned. These characters, except the one matching the next directive, are then compared to the locale values associated with the conversion specifier. If a match is found, values for the appropriate tm structure members are set to values corresponding to the locale information. Case is ignored when matching items in buf such as month or weekday names. If no match is found, strptime() fails and no more characters are scanned.


RETURN VALUE
Upon successful completion, strptime() returns a pointer to the character following the last character parsed. Otherwise, a null pointer is returned.
ERRORS
No errors are defined.
EXAMPLES
None.
APPLICATION USAGE
Several "same as" formats, and the special processing of white-space characters are provided in order to ease the use of identical format strings for strftime() and strptime().
Applications should use %Y (4-digit years) in preference to %y (2-digit years).

It is unspecified whether multiple calls to strptime() using the same tm structure will update the current contents of the structure or overwrite all contents of the structure. Portable applications should make a single call to strptime() with a format and all data needed to completely specify the date and time being converted.

FUTURE DIRECTIONS
This function is expected to be mandatory in the next issue of this specification.
SEE ALSO
scanf(), strftime(), time(), <time.h>.
LGame 2006-06-01
  • 打赏
  • 举报
回复
感谢你~~

还有没有
realorg 2006-06-01
  • 打赏
  • 举报
回复
唉,偶的时间类写的还有点问题,否则,就给你共享了~!
LGame 2006-06-01
  • 打赏
  • 举报
回复
太强了,

还有没有
LGame 2006-06-01
  • 打赏
  • 举报
回复
happytang(一只叫苏格拉底的猪)

你的
strptime(starttime, "%Y-%m-%d-%H-%M-%S", &tm1);

这个函数呢??

怎么把日期
给入结构??
jixingzhong 2006-06-01
  • 打赏
  • 举报
回复
上面的程序计算两个日期之间的 天数差别,

楼主再加工一下,
计算出 小时 和分钟 之差就可以达到你的要求了 ~
jixingzhong 2006-06-01
  • 打赏
  • 举报
回复
上面的程序计算两个日期之间的 天数差别,

楼主再加工一下,
计算出 小时 和分钟 之差就可以达到你的要求了 ~
jixingzhong 2006-06-01
  • 打赏
  • 举报
回复
#include<stdio.h>
#define judge(y) (y%4==0)&&(y%100!=0)||(y%400==0)
int allday(int x);
int month1[]={31,28,31,30,31,30,31,31,30,31,30,31};
int month2[]={31,29,31,30,31,30,31,31,30,31,30,31};

void main()
{ int i;

int yea1,yea2,mon1,mon2,day1,day2;
int *p;

long jia_day1=0;
long jia_day2=0;
long jia_day3=0;
long jia_day4=0;
long jia_all=0;

printf("请输入第一个年月份\n");
scanf("%d %d %d",&yea1,&mon1,&day1);
printf("请输入第二个年月份\n");
scanf("%d %d %d",&yea2,&mon2,&day2);
if(judge(yea1)) printf("%d是闰年\n",yea1);
if(judge(yea2)) printf("%d是闰年\n",yea2);
if(yea1>yea2)
{ if(judge(yea2)) p=month2;
else p=month1;
jia_day1=*(p+mon2-1)-day2;
for(i=mon2;i<=11;i++)
jia_day2=jia_day2+*(p+i);
for(i=yea2+1;i<yea1;i++)
jia_day3=jia_day3+allday(i);
if(judge(yea1)) p=month2;
else p=month1;
for(i=1;i<mon1;i++)
jia_day4=jia_day4+*(p+i-1);
jia_all=jia_day1+jia_day2+jia_day3+jia_day4+day1;
printf("%d%d%d比%d%d%d迟%d天\n",yea1,mon1,day1,yea2,mon2,day2,jia_all);
}
else if(yea1<yea2)
{ if(judge(yea1)) p=month2;
else p=month1;
jia_day1=*(p+mon1-1)-day1;
for(i=mon1;i<=11;i++)
jia_day2=jia_day2+*(p+i);
for(i=yea1+1;i<yea2;i++)
jia_day3=jia_day3+allday(i);
if(judge(yea2)) p=month2;
else p=month1;
for(i=1;i<mon2;i++)
jia_day4=jia_day4+*(p+i-1);
jia_all=jia_day1+jia_day2+jia_day3+jia_day4+day2;
printf("%d%d%d比%d%d%d迟%d天\n",yea2,mon2,day2,yea1,mon1,day1,jia_all);
}
else
{ if(judge(yea1)) p=month2;
else p=month1;
if(mon1>mon2)
{ jia_day1=*(p+mon2-1)-day2;
for(i=mon2+1;i<mon1;i++)
jia_day2=jia_day2+*(p+i-1);
jia_all=jia_day1+jia_day2+day1;
printf("第一个时间比第二个时间迟%d天\n",jia_all);
}
else if(mon1<mon2)
{ jia_day1=*(p+mon1-1)-day1;
for(i=mon1+1;i<mon2;i++)
jia_day2=jia_day2+*(p+i-1);
jia_all=jia_day1+jia_day2+day2;
printf("第一个时间比第二个时间早%d天\n",jia_all);
}
else
{if(day1>day2)
printf("第一个时间比第二个时间迟%d天\n",(day1-day2));
else if(day1<day2)
printf("第一个时间比第二个时间早%d天\n",(day2-day1));
else printf("这俩个时间相等\n");
}
}
}



int allday(int x)
{ int y;
if(((x%4==0)&&(x%100!=0))||((x%100==0)&&(x%400==0)))
{y=366; printf("在这俩个数据中:%d是闰年\n",x);}
else y=365;
return (y);
}
OpenHero 2006-06-01
  • 打赏
  • 举报
回复
如果是windows mfc下,可以用CTime和另一个函数直接处理
飞哥 2006-06-01
  • 打赏
  • 举报
回复
顶~
吃狼的豆腐 2006-05-31
  • 打赏
  • 举报
回复
路过
happytang 2006-05-31
  • 打赏
  • 举报
回复
同意楼上,方法像他说的,程序下面改一下自己,
这个是now时间与输入时间之差,精确到秒
计算时间差(C语言)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

void FormatRunTime(char* str,const char* starttime)
{

struct tm tm1,tm2;
time_t start,now;
double diff;
int days,hours,minitues,seconds;
assert(str && starttime);
strptime(starttime, "%Y-%m-%d-%H-%M-%S", &tm1);
start = mktime(&tm1);
time(&now);
diff=difftime(now,start);
days = (int)(diff/(3600*24));
hours =(int)((diff-days*3600*24)/3600);
minitues=(int)((diff-days*3600*24-hours*3600)/60);
seconds=(int)(diff-days*3600*24-hours*3600-minitues*60);
sprintf(str,"%d Day,%d Hour,%d Min,%d Sec",days,hours,minitues,seconds);
}

int main()
{
char ret[80];
FormatRunTime(ret,"1992-08-10-07-22-45");
printf("the result is %s\n",ret);
return 0;
}
YFY 2006-05-31
  • 打赏
  • 举报
回复
#include "time.h"

1 用mktime函数转换成time_t结构
2 用datediff函数获取2个time_t时间的秒数差
3 秒数差整除86400 3600 60的值即为相差的日 时 分

当然用mktime转化的时候你必须把字符串里的时间转化成tm结构的时间,这个应该比较简单了吧。
yingge 2006-05-31
  • 打赏
  • 举报
回复
struct time_diff {
long daydiff;
long hourdiff;
long mindiff;
};

void get_time_diff(const char *src,const char *dest,struct time_diff *td);

struct time_diff t;
get_time_diff("20060326 15:36:14","20060327 10:10:00",&t);

用一个好像上面的结构体纪录时间差,然后用上面的方法得到时间差。
get_time_diff函数我就不实现了,加班中,手头没有c编译器。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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