❶ 在c語言中如何獲取當前日期

#include
#include
void
main(){
struct
tm
*
tmptr;//時間的結構體
time_t
secnow;
time(&secnow);
tmptr
=
localtime(&secnow);
int
hour1,min1;
hour1
=
tmptr->tm_hour;
printf("the
time
is
%02d",hour1);//輸出當前小時-----------這才是人版家提問的要的時間的數字權!
}

❷ 如何用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。

❸ C語言中 如何獲取系統時間

方法一,#include<time.h>

int main()

{

time_t timep;

struct tm *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*/

}

方法二.#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;}

(3)c語言獲取當前時間擴展閱讀

1、CTimeSpan類

如果想計算兩段時間的差值,可以使用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();//獲取總共有多少秒

2、timeb()函數

_timeb定義在SYSTIMEB.H,有四個fields

dstflag

millitm

time

timezone

void _ftime( struct _timeb *timeptr );

struct _timeb timebuffer;

_ftime( &timebuffer );

❹ C語言中 如何獲取系統時間

#include <stdio.h>
#include <time.h>
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;}
(4)c語言獲取當前時間擴展閱讀
#include
--
必須的時間函數頭文件
time_t
--
時間類型(time.h
定義是typedef
long
time_t;
追根溯源,time_t是long)
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時間格式:
星期


時:分:秒

參考資料:網路
time函數

❺ C語言中如何獲取當前系統時間的小時

程序主要通過當前系統日歷的struct tm結構體獲得,主要代碼如下,
#include <stdio.h>
#include <time.h>
//程序功能輸出當前時間在24H下的小時數
int main(int argc, char *argv[])
{
struct tm *ptr;
time_t lt;
time(<);//當前系統時間
ptr=localtime(<);//獲取本地日歷時間指針
printf("hour=%d(24H )\n",ptr->tm_hour);//輸出24H下的小時數
return 0;
}

結構體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()為負。*/
long int tm_gmtoff; /*指定了日期變更線東面時區中UTC東部時區正秒數或UTC西部時區的負秒數*/
const char *tm_zone; /*當前時區的名字(與環境變數TZ有關)*/
};

❻ 用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()為負。*/
};

❼ 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

❽ 看過來,看過來 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*/
}