DateTime [上周,上个月]

时间:2010-02-02 06:16:56

标签: asp.net

我需要设置上周,上个月更改下拉列表的时间段

我正在制作

        switch (DDL.SelectedIndex)
        {
            case 0:
                {
                    // last week
                    this.TextBox3.Text = DateTime. //Previos week first day
                    this.TextBox4.Text = DateTime. //Previos week last day
                } break;
            case 1:
                {
                    // last mouth
                    this.TextBox3.Text = DateTime.// Previos month first day
                    this.TextBox4.Text = DateTime.// Previos month last day
                } break;
}

那么有哪些方法可以选择我想要的日期值?

另外,我在文本框上有AJAX日历扩展器

谢谢。

1 个答案:

答案 0 :(得分:3)

这样的事情,我想:

int dayOfWeekNumber = (int)DateTime.Today.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

var previosWeekFirstDay = DateTime.Today.AddDays(-7 - dayOfWeekNumber);

var previosWeekLastDay = previosWeekFirstDay.AddDays(6);

var previosMonthFirstDay = DateTime.Today.AddMonths(-1);
previosMonthFirstDay = previosMonthFirstDay.AddDays(-previosMonthFirstDay.Day + 1);

var previosMonthLastDay = previosMonthFirstDay.AddDays(DateTime.DaysInMonth(previosMonthFirstDay.Year, previosMonthFirstDay.Month) - 1);

已编辑:请参阅Fredrik Mörk评论。