//今天
DateTime.Now.Date.ToString(); //昨天 DateTime.Now.AddDays(-1).ToString(); //明天 DateTime.Now.AddDays(1).ToString(); //本週第1天及最後1天 (要注意的是這裡的每一週是從週日始至週六止) DateTime.Today.AddDays (-(int)DateTime.Today.DayOfWeek).ToString(); DateTime.Today.AddDays (6 -(int)DateTime.Today.DayOfWeek).ToString(); //上週, 一個周是7天,上週就是本週再減去7天 DateTime.Today.AddDays (-(int)DateTime.Today.DayOfWeek - 7).ToString(); DateTime.Today.AddDays (6 -(int)DateTime.Today.DayOfWeek - 7).ToString(); //下周 DateTime.Today.AddDays (-(int)DateTime.Today.DayOfWeek + 7).ToString(); DateTime.Today.AddDays (6 -(int)DateTime.Today.DayOfWeek + 7).ToString(); //巧用C#裡ToString的字符格式化 //本用第1天及最後1天 DateTime.Now.ToString("yyyy-MM-01"); DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToString(); //上個月第1天及最後1天 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToString(); DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToString(); //下個月第1天及最後1天 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToString(); DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToString(); //今年第1天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToString(); //今年最後1天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToString(); //去年第1天及最後1天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToString(); DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToString(); //明年第1天及最後1天 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToString(); DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToString(); //本季,一年四個季度,一個季度三個月 //先把日期推到本季度第一個月,然後這個月的第一天就是本季度的第一天了 DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01"); //本季度的最後一天就是下季度的第一天減一 DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToString(); //下季 DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01"); DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToString(); //上季 DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01"); DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToString(); |