C#中时间相关知识点小结
程序员文章站
2022-08-12 08:25:12
C#中时间相关知识点小结 一、月份英文简写 DateTime dt = DateTime.Now; string MM = dt.AddMonths(-1).ToString("MMM", new System.Globalization.CultureInfo("en-us"));//月英文缩写: ......
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; }
上一篇: 创建.Net 5应用并部署到Docker
下一篇: 母亲节笑话一串