我需要从标签上的日历控件打印选定的一周。所选周将从周一到周日。它必须看起来像这样:2015-09-29 - 2015-10-04。在此先感谢您的帮助!
以下是我到目前为止尝试过的代码,但此代码仅打印该周的各个日期。
DateTime input = Calendar1.SelectedDate;
int delta = DayOfWeek.Sunday - input.DayOfWeek;
DateTime firstDay = input.AddDays(delta);
Label3.Text = string.Empty;
for (int i = 0; i < 7; i++)
Label3.Text += ((DateTime)(firstDay.Add(new TimeSpan(i, 0, 0, 0)))).ToShortDateString() + " ";
答案 0 :(得分:0)
// Since Sunday has the index of 0, and you want the week to start from Monday,
// we need to shift the numbers around so that Monday is 0 instead.
int dayNumber = ((int)Calendar1.SelectedDate.DayOfWeek + 6) % 7;
DateTime monday = Calendar1.SelectedDate.AddDays(-dayNumber);
DateTime sunday = Calendar1.SelectedDate.AddDays(6 - dayNumber);
Label3.Text = string.Format(
"{0} - {1}",
monday.ToShortDateString(),
sunday.ToShortDateString());