C# DateTime 获取时间方法,网上收集

时间:2021-03-26 08:39:26
DateTime dt = DateTime.Now;  //当前时间
DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
DateTime endWeek = startWeek.AddDays(); //本周周日 DateTime startMonth = dt.AddDays( - dt.Day); //本月月初
DateTime endMonth = startMonth.AddMonths().AddDays(-); //本月月末// endMonth = startMonth.AddDays((dt.AddMonths() - dt).Days - ); //本月月末
DateTime startQuarter = dt.AddMonths( - (dt.Month - ) % ).AddDays( - dt.Day); //本季度初
DateTime endQuarter = startQuarter.AddMonths().AddDays(-); //本季度末 DateTime startYear = new DateTime(dt.Year, , ); //本年年初
DateTime endYear = new DateTime(dt.Year, , ); //本年年末至于昨天、明天、上周、上月、上季度、上年度等等, var 上周一 = DateTime.Now.AddDays(Convert.ToInt32( - Convert.ToInt32(DateTime.Now.DayOfWeek)) - ); //上周一
var 上周末 = DateTime.Now.AddDays(Convert.ToInt32( - Convert.ToInt32(DateTime.Now.DayOfWeek)) - ).AddDays(); //上周末(星期日)//下周
var 下周一 = DateTime.Now.AddDays(Convert.ToInt32( - Convert.ToInt32(DateTime.Now.DayOfWeek)) + ); //下周一
var 下周末 = DateTime.Now.AddDays(Convert.ToInt32( - Convert.ToInt32(DateTime.Now.DayOfWeek)) + ).AddDays(); //下周末 DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "").AddMonths().AddDays(-).ToShortDateString();//最后一天
//巧用C#里ToString的字符格式化更简便
DateTime.Now.ToString("yyyy-MM-01");//本月初
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths().AddDays(-).ToShortDateString();//本月最后一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-).ToShortDateString();//上个月1号
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-).ToShortDateString();//上个月最后一天
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths().ToShortDateString();// 下个月1号
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths().AddDays(-).ToShortDateString();//下下月最后一天
DateTime.Now.AddDays().ToShortDateString();//7天后
DateTime.Now.AddDays(-).ToShortDateString();//7天前
DateTime.Now.Date.ToShortDateString();//本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();//本年度第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears().AddDays(-).ToShortDateString();//本年度最后一天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-).ToShortDateString(); //上年度第一天,
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-).ToShortDateString();//上年度第最后一天, DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears().ToShortDateString(); //下年度第一天
DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears().AddDays(-).ToShortDateString();//下年度最后一天
//本季度,
DateTime.Now.AddMonths( - ((DateTime.Now.Month - ) % )).AddDays( - DateTime.Now.Day);//本季度第一天;
DateTime.Parse(DateTime.Now.AddMonths( - ((DateTime.Now.Month - ) % )).ToString("yyyy-MM-01")).AddDays(-).ToShortDateString();//本季度的最后一天
DateTime.Now.AddMonths( - ((DateTime.Now.Month - ) % )).ToString("yyyy-MM-01");//下季度的第一天
DateTime.Parse(DateTime.Now.AddMonths( - ((DateTime.Now.Month - ) % )).ToString("yyyy-MM-01")).AddDays(-).ToShortDateString();// 下季度最后一天 DateTime.Now.AddMonths(- - ((DateTime.Now.Month - ) % )).AddDays( - DateTime.Now.Day);// 上季度第一天
DateTime.Now.AddMonths( - ((DateTime.Now.Month - ) % )).AddDays( - DateTime.Now.Day).AddDays(-).ToShortDateString();// 上季度最后一天