當前位置:首頁 » 編程語言 » c語言怎麼獲取當前時間ms
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言怎麼獲取當前時間ms

發布時間: 2022-08-16 02:41:46

1. 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;

2. c語言如何獲取電腦實時時間

需要利用C語言的時間函數time和localtime,具體說明如下:

一、函數介面介紹:

1、time函數。

形式為time_t time (time_t *__timer);

其中time_t為time.h定義的結構體,一般為長整型。

這個函數會獲取當前時間,並返回。 如果參數__timer非空,會存儲相同值到__timer指向的內存中。

time函數返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。

由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函數了。

2、localtime函數。

形式為struct tm *localtime (const time_t *__timer);

其中tm為一個結構體,包含了年月日時分秒等信息。

這種結構是適合用來輸出的。

二、參考代碼:

#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//獲取Unix時間戳。
lt=localtime(&t);//轉為時間結構。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//輸出結果
return0;
}

注意事項:

struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt->tm_year+1900。

3. 請問在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
其它你一目瞭然啦。

4. 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;

}

5. c語言有辦法取得當前納秒或微秒級的時間嗎

C語言獲取當前系統時間的幾種方式
C語言獲取系統時間的幾種方式
C語言中如何獲取時間?精度如何?

1 使用time_t time( time_t * timer ) 精確到秒

2 使用clock_t clock() 得到的是CPU時間精確到1/CLOCKS_PER_SEC秒

3 計算時間差使用double difftime( time_t timer1, time_t timer0 )

4 使用DWORD GetTickCount() 精確到毫秒

5 如果使用MFC的CTime類,可以用CTime::GetCurrentTime() 精確到秒

6 要獲取高精度時間,可以使用

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)

獲取系統的計數器的頻率

BOOL QueryPerformanceCounter(LARGE_INTEGER
*lpPerformanceCount)

獲取計數器的值

然後用兩次計數器的差除以Frequency就得到時間。

7 Multimedia Timer Functions

The following functions are used with multimedia timers.

timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime

6. 13.5怎樣在C語言中取得當前日期或時間

C語言gmtime()函數:獲取當前時間和日期
頭文件:

#include<time.h>


定義函數:

structtm*gmtime(consttime_t*timep);


函數說明:gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm 返回。

結構tm 的定義為

structtm{
inttm_sec;//代表目前秒數,正常范圍為0-59,但允許至61秒
inttm_min;//代表目前分數,范圍0-59
inttm_hour;//從午夜算起的時數,范圍為0-23
inttm_mday;//目前月份的日數,范圍01-31
inttm_mon;//代表目前月份,從一月算起,范圍從0-11
inttm_year;//從1900年算起至今的年數
inttm_wday;//一星期的日數,從星期一算起,范圍為0-6
inttm_yday;//從今年1月1日算起至今的天數,范圍為0-365
inttm_isdst;//日光節約時間的旗標
};


此函數返回的時間日期未經時區轉換,而是UTC 時間。

返回值:返回結構tm 代表目前UTC 時間。

範例

#include<time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d%d%d",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
printf("%s%d;%d;%d ",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
}


執行結果:

2000/10/28Sat8:15:38

7. 問在C語言里怎麼獲取當前時間和日期

#include <time.h> 要添加這個頭文件。

time_t rawtime;
struct tm * target_time;
time ( &rawtime ); //獲取當前時間,存rawtime里
target_time = localtime ( &rawtime ); //獲取當地時間
利用struct tm,你可以按需取出年月日時分秒星期幾等數值。
---------------------
你的問題:
time_t now;
long int dt=3600; // 時間長度,秒數
now = time (NULL); //獲取當前時間
printf("%s ",ctime(&now)); //直接列印時間
now=now+dt;
printf("%s ",ctime(&now)); // 直接列印加dt後的時間

(當然,你也可以用 ctime(&now) 返回的字元串 通過 MFC 的方法顯示)

8. C語言如何獲得精確到毫秒的時間

C語言里沒有標準的介面可以獲得精確到毫秒的時間,只有調用到與操作系統相關的API才可以

9. 看過來,看過來 C語言獲取系統時間的幾種方式

我們在寫C語言程序的時候,有的時候會用到讀取本機的時間和日期,怎麼做呢?其實很簡單的,下面簡單說一下:

C語言中讀取系統時間的函數為time(),其函數原型為:#include <time.h>time_t time( time_t * ) ;

time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。

可以調用ctime()函數進行時間轉換輸出:char * ctime(const time_t *timer);

將日歷時間轉換成本地時間,按年月日格式,進行輸出,如:Wed Sep 23 08:43:03 2015C語言還提供了將秒數轉換成相應的時間結構的函數:

struct tm * gmtime(const time_t *timer);//將日歷時間轉化為世界標准時間(即格林尼治時間)

struct tm * localtime(const time_t * timer);//將日歷時間轉為本地時間將通過time()函數返回的值,轉成時間結構structtm :

struct tm {int tm_sec; /* 秒 – 取值區間為[0,59] */

int tm_min; /* 分 - 取值區間為[0,59] */

int tm_hour; /* 時 - 取值區間為[0,23] */

int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */

int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */

int tm_year; /* 年份,其值等於實際年份減去1900 */

int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */

int tm_yday; /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */

int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/};

編程者可以根據程序功能的情況,靈活的進行日期的讀取與輸出了。

下面給出一段簡單的代碼:

#include<time.h>
intmain()
{
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec);/*獲取當前秒*/
printf("%d ",p->tm_min);/*獲取當前分*/
printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d ",p->tm_yday);/*從今年1月1日算起至今的天數,范圍為0-365*/
}

10. 用c語言如何獲取系統當前時間的函數

1、C語言中讀取系統時間的函數為time(),其函數原型為:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。
2、C語言還提供了將秒數轉換成相應的時間格式的函數:
char * ctime(const time_t *timer); //將日歷時間轉換成本地時間,返回轉換後的字元串指針 可定義字元串或是字元指針來接收返回值
struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標准時間(即格林尼治時間),返回結構體指針 可定義struct tm *變數來接收結果
struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間,返回結構體指針 可定義struct tm *變數來接收結果
3、常式:
#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}

時間結構體struct tm 說明:

struct tm {
int tm_sec; /* 秒 – 取值區間為[0,59] */
int tm_min; /* 分 - 取值區間為[0,59] */
int tm_hour; /* 時 - 取值區間為[0,23] */
int tm_mday; /* 一個月中的日期 - 取值區間為[1,31] */
int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
int tm_year; /* 年份,其值等於實際年份減去1900 */
int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */
int tm_yday; /* 從每年的1月1日開始的天數 – 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};