从标签上的“日历”控件打印选定的星期

本文关键字:打印 控件 标签 日历 | 更新日期: 2023-09-27 18:14:44

我需要从标签上的日历控件打印所选的星期。选择的周为周一至周日。它应该是这样的: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() + "  ";

从标签上的“日历”控件打印选定的星期

// 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());