c# datetime方法應用介紹

來源:互聯網
上載者:User

隨著工作的需要,也算是寫一個為自己留著的協助文檔吧。
System.DateTime currentTime=new System.DateTime(); //執行個體化一個 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; (變數可用中文)
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();//明天
中文日期 年月日時分 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");
更多格式看 附1、2。

//本周 (注意這裡的每一周是從周日始至周六止)
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();//最後一天
--------------------------------------------------------------------------------
附1:datetime類型在tostring(),format的格式設定

格式字元 關聯屬性/說明
d ShortDatePattern
D LongDatePattern
f 完整日期和時間(長日期和短時間)
F FullDateTimePattern(長日期和長時間)
g 常規(短日期和短時間)
G 常規(短日期和長時間)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用當地時間的 SortableDateTimePattern(基於 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用於顯示通用時間的格式
U 使用通用時間的完整日期和時間(長日期和長時間)
y、Y y、Y YearMonthPattern

附2:下表列出了可被合并以構造自訂模式的模式。這些模式是區分大小寫

格式字元 關聯屬性/說明
d 月中的某一天。一位元的日期沒有前置字元為零。
dd 月中的某一天。一位元的日期有一個前置字元為零。
ddd 周中某天的縮寫名稱,在 AbbreviatedDayNames 中定義。
dddd 周中某天的完整名稱,在 DayNames 中定義。
M 月份數字。一位元的月份沒有前置字元為零。
MM 月份數字。一位元的月份有一個前置字元為零。
MMM 月份的縮寫名稱,在 AbbreviatedMonthNames 中定義。
MMMM 月份的完整名稱,在 MonthNames 中定義。
y 不包含紀元的年份。如果不包含紀元的年份小於 10,則顯示不具有前置字元為零的年份。
yy 不包含紀元的年份。如果不包含紀元的年份小於 10,則顯示具有前置字元為零的年份。
yyyy 包括紀元的四位元的年份。
gg 時期或紀元。如果要設定格式的日期不具有關聯的時期或紀元字串,則忽略該模式。
h 12小時制的小時。一位元的小時數沒有前置字元為零。
hh 12 小時制的小時。一位元的小時數有前置字元為零。
H 24 小時制的小時。一位元的小時數沒有前置字元為零。
HH 24 小時制的小時。一位元的小時數有前置字元為零。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.