C语言~时间函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言~时间函数相关的知识,希望对你有一定的参考价值。
求现在的年月日时分秒到2012年12月21日00时00分00秒,多少年月日时分秒
这个试看看应该可以的#include <stdio.h>;
#include <time.h>;
time_t scanf_time(char * timestr)
struct tm t;
if(!timestr) return 0;
memset(&t, 0, sizeof(t));
sscanf(timestr, "%02d%02d%02d %02d:%02d:%02d",
&(t.tm_mday), &(t.tm_mon), &(t.tm_year),
&(t.tm_hour), &(t.tm_min), &(t.tm_sec));
t.tm_year += 100;
t.tm_mon -=1;
return mktime(&t);
int main(int argc, char *argv[])
time_t x = 0;
time_t y = 0;
x = scanf_time("160312 00:00:00");
y = time(NULL);
if(x>y)
//0=1970-00-01 08:00:00
struct tm t;
memcpy(&t, localtime(&x), sizeof(struct tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n", t.tm_year + 1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
memcpy(&t, localtime(&y), sizeof(struct tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n", t.tm_year + 1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
time_t result = x - y;
memcpy(&t, localtime(&result), sizeof(struct tm));
printf("\n%d年-%d月-%d日\t%d时:%d分:%d秒\n", t.tm_year -70, t.tm_mon, t.tm_mday-1, t.tm_hour-8, t.tm_min, t.tm_sec);
system("PAUSE");
return 0;
参考技术A #include <time.h>
#include <stdio.h>
int main( void )
struct tm *stNow , st2012 ;
time_t now;
int year,mon,day,hour,min,sec ;
st2012.tm_year = 2012 - 1900;
st2012.tm_mon = 12 - 1;
st2012.tm_mday = 1;
st2012.tm_hour = 0;
st2012.tm_min = 0;
st2012.tm_sec = 0;
now=time(&now);
stNow=localtime(&now) ;
year= st2012.tm_year - stNow->tm_year ;
mon= st2012.tm_mon - stNow->tm_mon ;
day= st2012.tm_mday - stNow->tm_mday ;
hour= st2012.tm_hour - stNow->tm_hour ;
min= st2012.tm_min - stNow->tm_min ;
sec= st2012.tm_sec - stNow->tm_sec;
if ( sec < 0 )
sec += 60 , min -= 1;
if ( min < 0 )
min += 60 , hour -= 1;
if ( hour < 0 )
hour += 24 , day -= 1;
if ( day < 0 )
day += 30 , mon -= 1; //因为目标月是12月,所以,只考虑11月的天数
if ( mon < 0 )
mon += 12 , year -= 1;
printf("diff %d年%d月%d日%d时%d分%d秒\n" , year ,mon,day,hour,min,sec );
return 0;
参考技术B printf("the current in sting format is : %s", asctime(ts));
struct tm *tsgm = gmtime(&curTime);
printf("the current in CUT format string is : %s ", asctime(tsgm));
char strTimebuf[1000];
printf("the full use of strftime function: ");
strftime(strTimebuf, 1000, "now the abbreviated weekday time is %a.
the full weekday name is %A. the abbreviated month name is %b.
the full month name is %B. the standard date and time string is %c.
the day of the month as a number is %d. the 24 format hour is %H.
the 12 format hour is %I. the day of the year is %j.
the month number is %m. the minute number is %M.
the locale’s equivalent of AM or PM is %p. the second number is %S.
the week of the year is %U(Monday is the first). the decimal weekday is %w.
the week of the year is %W(Sunday is the first). the standard date string is %x.
the year in decimal without century is %y. the year in decimal with century is %Y.
以上是关于C语言~时间函数的主要内容,如果未能解决你的问题,请参考以下文章