
#include <stdioh>
#include <timeh>
int main( )
{
time_t nowtime;
struct tm timeinfo;
time( &nowtime );
timeinfo = localtime( &nowtime );
int year, month, day;
year = timeinfo->tm_year + 1900;
month = timeinfo->tm_mon + 1;
day = timeinfo->tm_mday;
printf("%d %d %d\n", year, month, day);
return 0;
}
struct tm -- 时间结构,timeh 定义如下:
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;
tm_year 从1900年计算,所以要加1900,
月tm_mon,从0计算,所以要加1
time( &nowtime ); 获取时间
localtime ( &nowtime ); 转为当地时间
#include
#include
int main ()
{
time_t t
struct tm lt; time (&t);//获取Unix时间戳。
lt = localtime (&t);//转为时间结构。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
return 0;}
扩展资料
#include
--
必须的时间函数头文件
time_t
--
时间类型(timeh
定义是typedef
long
time_t;
追根溯源,time_t是long)
struct
tm
--
时间结构,timeh
定义如下:
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时间格式:
星期
月
日
时:分:秒
年
参考资料:
time函数
#include <stdioh>
#include <timeh>
int main(void)
{
time_t rawtime;
struct tm timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("%s", asctime(timeinfo));
return 0;
}
代码如下:
#include <stdioh>#include <stdlibh>
#include <timeh>
int main()
{
// 获取当前时间
tm tm;
_getsystime(&tm);
// 将当前时间转换成字符串并输出
char str[100];
strftime(str, sizeof(str), "%c", &tm);
printf("%s\n", str);
system("pause");
return 0;
}
运行结果:
1用difftime,秒级
#include <timeh>
#include <stdioh>
#include <dosh>
int main(void)
{
time_t first, second;
first = time(NULL); / Gets system time /
// 你的程序
second = time(NULL); / Gets system time again /
printf("The difference is: %f seconds\n",difftime(second,first));
getch();
return 0;
}
2用DWORD GetTickCount(VOID)
CTime WINAPI GetCurrentTime( ) throw( );
获得更精确的时间 GetTickCount
3获取系统编译程序的时间
char time1 = __DATE__;
char time2 = __TIME__;
其中__DATE__和__TIME__是俩个宏。
#include <stdioh>
#include <timeh>
int main( )
{
time_t nowtime;
struct tm timeinfo;
time( &nowtime );
timeinfo = localtime( &nowtime );
int year, month, day;
year = timeinfo->tm_year + 1900;
month = timeinfo->tm_mon + 1;
day = timeinfo->tm_mday;
printf("%d %d %d\n", year, month, day);
return 0;
}
以上就是关于在C语言环境下怎么获取系统当前时间然后返回值啊全部的内容,包括:在C语言环境下怎么获取系统当前时间然后返回值啊、C语言中 如何获取系统时间、C语言如何获取系统时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)