现在的位置: 首页 > 综合 > 正文

c/c++获取本机时间

2013年08月15日 ⁄ 综合 ⁄ 共 407字 ⁄ 字号 评论关闭

通过vs2008编译运行

#include <iostream>
#include <string>
#include<time.h>//必须包含time.h
int main()
{
    struct tm *tm;//需要定义tm结构体,存放时间信息
    time_t now;
    now = time(NULL);//注意调用方式
    tm = localtime( static_cast<const time_t*>(&now) );//需要传递time_t地址
    printf("%02d:%02d:%02d   %02d/%02d/%4d\n"
          ,tm->tm_hour,tm->tm_min,tm->tm_sec,tm->tm_mday
          ,tm->tm_mon+1,tm->tm_year+1900);//月份要加上1,年份要加上1900
    system("pause");
    return 0;
}

抱歉!评论已关闭.