您好,登錄后才能下訂單哦!
本篇內容主要講解“C#常用日期時間方法有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C#常用日期時間方法有哪些”吧!
一、月份英文簡寫
DateTime dt = DateTime.Now; string MM = dt.AddMonths(-1).ToString("MMM", new System.Globalization.CultureInfo("en-us"));//月英文縮寫:Jul
二、當月第一天和最后一天
DateTime ThisMonth_Frist = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date; DateTime ThisMOnth_Last = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
三、上月第一天和最后一天
DateTime Today = DateTime.Today;//當天時間 DateTime ThisMonth = new DateTime(Today.Year, Today.Month, 1);//當前月第一天時間 DateTime LastMonth_First = ThisMonth.AddMonths(-1);//上月第一天時間 DateTime LastMonth_Last = ThisMonth.AddDays(-1);//上月最后一天時間
四、本周第幾天
int daysInWeek1 = (int)DateTime.Now.DayOfWeek;//注意:此處周,日時返回0, int daysInWeek2 = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//當前周第幾天,注釋:周日為0
五、本月第幾周
int a = WeekOfMonth(DateTime.Now, false);//
六、本年第幾周
private int WeekOfYear() { var dt = DateTime.Now; int firstWeekend = Convert.ToInt32(DateTime.Parse(dt.Year + "-1-1").DayOfWeek); int weekDay = firstWeekend == 0 ? 1 : (7 - firstWeekend + 1); int currentDay = dt.DayOfYear; int current_week = Convert.ToInt32(Math.Ceiling((currentDay - weekDay) / 7.0)) + 1; return current_week; }
七、前幾周的周一和周日
private void FEDayInLastWeek() { int N = 3;//前幾周參數 DateTime Today = DateTime.Now; int daysInWeek = (int)Today.DayOfWeek == 0 ? 7 : (int)Today.DayOfWeek;//當前周第幾天,注釋:周日為0 for (int i = N; i > 0; i--) { //起始日期 DateTime firstDay = Today.AddDays(1 - (7 * i + daysInWeek)); DateTime lastDay = Today.AddDays(7 - (7 * i + daysInWeek)); } }
八、本周一和當前日
private void FristDayToNowInThisWeek() { int daysInWeek = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//當前周第幾天,注釋:周日為0 //起始日期 DateTime firstDay = DateTime.Now.AddDays(1 - daysInWeek); DateTime lastDay = DateTime.Now; }
C#中控件綁定時間欄位格式化
Bind("查詢出的欄位", "{0:yyyy/MM/dd}")
到此,相信大家對“C#常用日期時間方法有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。