『壹』 如何用c語言編寫一個顯示時間的函數,要求時間顯示精度到毫秒級別。
#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 ", 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();
}
『貳』 C語言如何獲取本地時間,然後取時、分、秒的值
#include <stdio.h>
#include <time.h>
int main()
{time_t timep;
struct tm *tp;
time(&timep);
int p;
tp = localtime(&timep); //取得系統時間
printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);
printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);
p=tp->tm_sec;
printf("p=%d ",p);
return 0;
}
『叄』 C語言獲得當前系統時間,只顯示小時、分鍾、秒
#include "dos.h "
main()
{
struct date local_date;
struct time local_tiem;
gettime(local_time);
getdate(local_date);
printf( "%d-%d-%d %d:%d:%d ",local_date-> da_year,local-> da_mon,local-> da_day\
local_time-> ti_hour,local-> ti_min,local_time-> ti_sec);
return 0;
}
如果要MS的話就加一個local_time-> ti_hund;
『肆』 c語言怎麼獲取電腦當前時間的分鍾
# include <stdio.h>
# include <time.h>
int main(void)
{
time_t timer = time(NULL);
printf("ctime is %s\n", ctime(&timer));
return 0;
}
time獲取時間是1970年1月1日到現在的秒數,ctime將秒數轉成時間字元串。
『伍』 c如何獲取精確到毫秒的時間
1.使用CTime類(獲取系統當前時間,精確到秒)
CString str;
//獲取系統時間
CTime tm;
tm=CTime::GetCurrentTime();//獲取系統日期
str=tm.Format("現在時間是%Y年%m月%d日 %X");
MessageBox(str,NULL,MB_OK);
a,從CTimet中提取年月日時分秒
CTime t = CTime::GetCurrentTime();
int d=t.GetDay(); //獲得幾號
int y=t.GetYear(); //獲取年份
int m=t.GetMonth(); //獲取當前月份
int h=t.GetHour(); //獲取當前為幾時
int mm=t.GetMinute(); //獲取分鍾
int s=t.GetSecond(); //獲取秒
int w=t.GetDayOfWeek(); //獲取星期幾,注意1為星期天,7為星期六
b,計算兩段時間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計算當前系統時間與時間t1的間隔
int iDay=span.GetDays(); //獲取這段時間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時
int iMin=span.GetTotalMinutes();//獲取總共有多少分鍾
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
c,獲得當前日期和時間,並可以轉化為CString
CTime tm=CTime::GetCurrentTime(); CString str=tm.Format("%Y-%m-%d");//顯示年月日
2.使用GetLocalTime:Windows API 函數,獲取當地的當前系統日期和時間 (精確到毫秒)
此函數會把獲取的系統時間信息存儲到SYSTEMTIME結構體里邊
typedef struct _SYSTEMTIME
{
WORD wYear;//年
WORD wMonth;//月
WORD wDayOfWeek;//星期:0為星期日,1為星期一,2為星期二……
WORD wDay;//日
WORD wHour;//時
WORD wMinute;//分
WORD wSecond;//秒
WORD wMilliseconds;//毫秒
}SYSTEMTIME,*PSYSTEMTIME;
『陸』 請問在C語言里怎麼獲取當前時間和日期(精確到毫秒)
先申明下,這個是我轉網路知道的,經常BAIDU一下,就OK了
#include <stdio.h>
#include <time.h>
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include <time.h> -- 必須的時間函數頭文件
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時間格式:
星期 月 日 時:分:秒 年
=========================================
你要的格式可這樣輸出:
printf ( "%4d-%02d-%02d %02d:%02d:%02d\n",1900+timeinfo->tm_year, 1+timeinfo->tm_mon,
timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
就是直接列印tm,tm_year 從1900年計算,所以要加1900,
月tm_mon,從0計算,所以要加1
其它你一目瞭然啦。
『柒』 在C語言中如何實現精確計時
time()
頭文件:time.h
函數原型:time_t time(time_t * timer)
功能:返回以格林尼治時間(GMT)為標准,從1970年1月1日00:00:00到現在的此時此刻所經過的秒數。
2.clock()
頭文件:time.h
函數原型:clock_t clock(void);
功能:該函數返回值是硬體滴答數,要換算成秒,需要除以CLK_TCK或者 CLK_TCKCLOCKS_PER_SEC。比如,在VC++6.0下,這兩個量的值都是1000。
3. timeGetTime()
頭文件:Mmsystem.h 引用庫: Winmm.lib
函數原型:DWORD timeGetTime(VOID);
功能:返回系統時間,以毫秒為單位。系統時間是從系統啟動到調用函數時所經過的毫秒數。注意,這個值是32位的,會在0到2^32之間循環,約49.71天。