❶ 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 2015
C語言還提供了將秒數轉換成相應的時間結構的函數:
struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標准時間(即格林尼治時間)
struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間
將通過time()函數返回的值,轉換成時間結構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()為負。*/
};
編程者可以根據程序功能的情況,靈活的進行日期的讀取與輸出了。
例如:
#include<time.h>
main()
{
time_t timep;
struct tm *p;
time (&timep);
p=gmtime(&timep);
printf("%d\n",p->tm_sec); /*獲取當前秒*/
printf("%d\n",p->tm_min); /*獲取當前分*/
printf("%d\n",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d\n",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d\n",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d\n",p->tm_yday); /*從今年1月1日算起至今的天數,范圍為0-365*/
}
❷ 如何用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# Windows窗體 狀態欄上顯示系統當前時間。應該如何實現
DateTime.Now.Tostring();
❹ C#如何寫程序:「顯示當前時間」
C#中獲取當前時間方法:
DateTime 數字型
System.DateTime currentTime=new System.DateTime();
取當前年月日時分秒 currentTime=System.DateTime.Now;
取當前年 int 年=currentTime.Year;
取當前月 int 月=currentTime.Month;
取當前日 int 日=currentTime.Day;
取當前時 int 時=currentTime.Hour;
取當前分 int 分=currentTime.Minute;
取當前秒 int 秒=currentTime.Second;
取當前毫秒 int 毫秒=currentTime.Millisecond; (變數可用中文)
取中文日期顯示——年月日時分 string strY=currentTime.ToString("f"); //不顯示秒
取中文日期顯示_年月 string strYM=currentTime.ToString("y");
取中文日期顯示_月日 string strMD=currentTime.ToString("m");
取當前年月日,格式為:2003-9-23 string strYMD=currentTime.ToString("d");
取當前時分,格式為:14:24 string strT=currentTime.ToString("t");
DateTime.Now.ToString();//獲取當前系統時間完整的日期和時間
DateTime.Now.ToLongDateString();//只顯示日期 xxxx年xx月xx日,一個是長日期
DateTime.Now.ToShortDateString();//只顯示日期 xxxx-xx-xx 一個是短日期
//今天 DateTime.Now.Date.ToShortDateString();
//昨天的 DateTime.Now.AddDays(-1).ToShortDateString();
//明天的 DateTime.Now.AddDays(1).ToShortDateString();
//本周(注意這里的每一周是從周日始至周六止)
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
//上周,上周就是本周再減去7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
//下周 本周再加上7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
//本月 本月的第一天是1號,最後一天就是下個月一號再減一天。
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最後一天
另一種方法:
DateTime now = DateTime.Now;
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天
DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最後一天
PS:
DateTime.Now.DayOfWeek.ToString();//英文星期顯示,Wednesday
(int)DateTime.Now.DayOfWeek 數字,若是周三,結果對應為3
DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期顯示
DateTime.Now.ToString("dddd");//中文星期顯示
DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//顯示日期格式Friday,July, 01,2009
DateTime.Now.ToString("dddd,dd MMMM,yyyy") //輸出 星期三,30 一月,2008
❺ c++語言中如何實現顯示系統時間
c++語言中可調用time()函數獲得一個時間值,該時間值是從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數,數據類型為long。可以調用ctime()函數進行時間轉換輸出。
如果進行停車收費計算和顯示,可如下操作:
定義兩個long類型變數,in_time, out_time;用存儲進場時間和離場時間
用out_time-in_time得到停車時間(秒),根據自定義收費規則進行計費
用ctime( &in_time)來顯示可視進場時間
用ctime( &out_time)來顯示可視進場時間
參考代碼如下:
#include <ctime>
#include <iostream>
#include <windows.h>
using namespace std ;
void main(void)
{
long in_time, out_time;
time(&in_time); //得到入場時間
cout << "in time: " <<ctime(&in_time) <<endl ;
//停留
Sleep(3000); //模擬停三秒
time(&out_time);
cout << "out time: " <<ctime(&out_time) <<endl ;
cout << "stop time: " <<out_time-in_time <<endl ;
}
❻ c語言設計的系統中怎麼顯示實時的時間,
可以調用 time.h 里的時間函數顯示 實時的時間。
例如:
#include <stdio.h>
#include <time.h>
int main(){
time_t t;
struct tm * timeinfo;
time(&t);
timeinfo = localtime ( &t );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}
調用DOS date time 命令顯示 日期和時間也可以:
system("date /T"); system("time /T");
❼ C語言中怎樣調用系統時間並動態顯示!
void Time() //系統時間
{
printf("\n\n************* 歡迎進入*********系統 *************\n");
printf("\n\n 版本所屬: ****** \n");
printf("\n\n *****\n\n ");
CString sDate;
CString *p;
p = &sDate;
while(1)
{
CTime Now=CTime::GetCurrentTime();
*p=Now.Format("%Y年 %m月 %d日 %H時 %M分 %S秒");
printf("%s", *p);
Sleep(1000);
for(int i=1; i<=strlen(*p); i++)
{
printf("\b"); //
printf(" ");
printf("\b");
}
}
printf("\n *******\n");
}
當運行之後,雖然實現了 實現動態顯示當前系統時間, 但是無法繼續執行 Time函數後面的程序了,即無法繼續執行下面搖獎程序了。
如果是使用MFC來做,那麼這種問題是不存在的,直接使用C語言,運行於DOS窗口。
❽ 用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;}
(8)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>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));
return0;
}
time_t//時間類型(time.h定義)
structtm{//時間結構,time.h定義如下:
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
}
time(&rawtime);//獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime(&rawtime);//轉為當地時間,tm時間結構
asctime()//轉為標准ASCII時間格式:
//就是直接列印tm,tm_year從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1