C 常用日期時間函式(老用不熟)

2022-09-26 09:54:33 字數 2534 閱讀 4705

--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天,上週就www.cppcns.com是本週再減去7天,下週也是一樣

datetime.now.adddays(conver程式設計客棧t.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# 常用日期時間函式(老用不熟)

本文位址: /ruanjian/csharp/51622.html

C 常用日期時間函式

datetime 數字型 system.datetime currenttime new system.datetime 1.1 取當前年月日時分秒 currenttime system.datetime.now 1.2 取當前年 int 年 currenttime.year 1.3 取當前月 in...

C 常用日期函式

我想知道取的時期是幾月 幾日,然後做一些統計,上網找了一些方法。datetime 數字型 system.datetime currenttime new system.datetime 1.1 取當前年月日時分秒 currenttime system.datetime.now 1.2 取當前年 in...

MYSQL常用函式(時間和日期函式)

curdate 或current date 返回當前的日期 curtime 或current time 返回當前的時間 date add date,interval int keyword 返回日期date加上間隔時間int的結果 int必須按照關鍵字進行格式化 如 selectdate add c...