开发者

DateTime [ Last week , Last month periods ]

开发者 https://www.devze.com 2022-12-19 09:17 出处:网络
I need to setup last week , last month periods on changing dropdownlist I\'m making switch (DDL.SelectedIndex)

I need to setup last week , last month periods on changing dropdownlist

I'm making

        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;
}

So is there some ways how can I select date values like I want ?

also , I've got AJAX calendar extender on text boxes

thank you.


Something like this, I think:

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

Edited: see Fredrik Mörk comment.

0

精彩评论

暂无评论...
验证码 换一张
取 消