用C 獲取系統時間

2021-05-10 11:31:00 字數 2401 閱讀 7371

用c#獲取系統時間

--datetime 數字型

system.datetime currenttime=new system.datetime();

1.1 取當前年月日時分秒

currenttime=system.datetime.now;

1.2 取當前年

int 年=currenttime.year;

1.3 取當前月

int 月=currenttime.month;

1.4 取當前日

int 日=currenttime.day;

1.5 取當前時

int 時=currenttime.hour;

1.6 取當前分

int 分=currenttime.minute;

1.7 取當前秒

int 秒=currenttime.second;

1.8 取當前毫秒

int 毫秒=currenttime.millisecond;

(變數可用中文)

1.9 取中文日期顯示_年月日時分

string stry=currenttime.tostring("f"); //不顯示秒

1.10 取中文日期顯示_年月

string strym=currenttime.tostring("y");

1.11 取中文日期顯示_月日

string strmd=currenttime.tostring("m");

1.12 取當前年月日,格式為:2003-9-23

string strymd=currenttime.tostring("d");

1.13 取當前時分,格式為:14:24

string strt=currenttime.tostring("t");

//今天

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();

//如果你還不明白,再看一下中文顯示星期幾的方法就應該懂了

//由於dayofweek返回的是數字的星期幾,我們要把它轉換成漢字方便我們閱讀,有些人可能會用switch來乙個乙個地對照,其實不用那麼麻煩的

string day = new string ;

day[convert.toint16(datetime.now.dayofweek)];

//上週,同理,乙個週是7天,上週就是本週再減去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();

//下週

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();//最後一天

C 系統時間獲取

在c 中想要獲取當前系統的時候可以使用函式 time t time time t timer 使用這個函式如果傳入的 引數不是null 那麼,它就會把當前系統的時間設定到這個指標當中 這個函式返回的 數字是 從 00 00 hours,jan 1,1970 utc 的 秒 struct tm loc...

c 獲取系統時間

方案 優點 僅使用c標準庫 缺點 只能精確到秒級 include include int main void size t strftime char strdest,size t maxsize,const char format,const struct tm timeptr 根據格式字串生成字...

C 獲取系統時間

include include using namespace std int main 說明 struct tm 在vc 中,我們可以借助ctime時間類,獲取系統當前日期 ctime t ctime getcurrenttime 獲取系統日期 int d t.getday 獲得幾號 int y ...