如果根据当前日期获取当前周日期和当前周日期

来源:百度知道 编辑:UC知道 时间:2024/06/14 04:25:48
DateTime.Now.ToShortDateString();可以获得当前是几月几日.
那请问:我如何根据当前的日期,获取这周是几号到几号.这个月是几号到几号呢?

很抱歉楼上你给的代码是错误的。。

//这里把星期天当作一周的开始
Console.WriteLine(
"本周起始日期:{0},结束日期:{1}",
DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek).ToShortDateString(),
DateTime.Now.AddDays(6-(int)DateTime.Now.DayOfWeek).ToShortDateString());

Console.WriteLine(
"本月共 {0} 天,起始日期:{1},结束日期:{2}",
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month),
DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString(),
DateTime.Now.AddDays(DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) - DateTime.Now.Day).ToShortDateString()
);

很简单,一会给你做出来了。。。。。。
给你代码

DateTime date = DateTime.Now;
MessageBox.Show("这周是" + date.ToShortDateString().ToString() + "到" + date.AddDays(7).ToShortDateString().ToString());

MessageBox.Show("这个月是" + date.ToShortDateString().ToString() + "到" + date.AddMonths(1).ToShortDateString()