本篇内容主要讲解“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#常用日期时间方法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!