c语言 打印输出时分秒毫秒

Python016

c语言 打印输出时分秒毫秒,第1张

#include <stdio.h>

#include <time.h>

int main()

{

time_t rawtime

struct tm * timeinfo

time ( &rawtime )

timeinfo = localtime ( &rawtime )

printf ( "The current date/time is: %s", asctime (timeinfo) )

return 0

}

说明:

time_t // 时间类型(time.h 定义

struct tm {// 时间结构,time.h 定义如下:

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时间格式:

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

#include <cstdio>

#include <ctime>

using namespace std

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void printTime() {

  struct tm t   //tm结构指针

time_t now  //声明time_t类型变量

time(&now)      //获取系统日期和时间

localtime_s(&t, &now)   //获取当地日期和时间

   //格式化输出本地时间

printf("年-月-日-时-分-秒:%d-%d-%d %d:%d:%d\n", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)

}

int main(int argc, char** argv) {

printTime()

}

用clock就到毫秒了. 它是直接返回毫秒.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

{

clock_t start, finish

double elapsed_time

start=clock()

finish=clock()

elapsed_time = finish-start

}