asp.net:计算某段时间的日期范围

时间:2011-03-11 20:47:42

标签: asp.net

我有一个下拉列表,其值为'今天','昨天',上周','上个月','去年'。

当用户从此下拉列表中选择任何值时,我想在两个文本框中显示日期和日期。

e.g。如果用户选择上周,“到日期”应该是今天的日期,“从日期”应该是(今天的日期 - 7)。如果当前日期为12,则“从日期”= 6并且“到日期”= 12

我该怎么做?

3 个答案:

答案 0 :(得分:2)

应该是这样的:

var toDate= DateTime.Now;
var fromDate = DateTime.Today;

switch(dropDown.SelectedValue.ToLower())
{
    case "today": fromDate = toDate; break;
    case "yesterday": fromDate = toDate.AddDays(-1); break;
    case "lastweek": fromDate = toDate.AddDays(-(int)toDate.DayOfWeek); break;
    case "last month": fromDate = toDate.AddMonths(-1); break;
    case "lastyear": fromDate = toDate.AddYears(-1); break;
    default: break;
}

ddlFrom.Text = fromDate.ToShortDateString();
ddlTo.Text = toDate.ToShortDateString();

答案 1 :(得分:0)

最简单的方法是:

FromDateTextBox.Text = DateTime.Today.ToString();

if(YourDropDown.SelectedValue == "today")
  ToDateTextBox.Text = DateTime.Today.ToString();
else if (YourDropDown.SelectedValue == "yesterday")
  ToDateTextBox.Text = DateTime.Today.AddDays(-1).ToString();
// etc.

答案 2 :(得分:0)

我不确定你是否想要本周(今天 - 今天6天至今)或今天 - 7天。另一种选择是从星期一(或每周开始)到现在,月份和年份(可能是2011/01/01)相同。

ASPX:

<asp:DropDownList ID="DdlTimeSpan" OnSelectedIndexChanged="DdlTimeSpan_SelectedIndexChanged" AutoPostBack="true" runat="server">
           <asp:ListItem Text="-- select timespan --" Value="0"></asp:ListItem>
           <asp:ListItem Text="today" Value="today"></asp:ListItem>
           <asp:ListItem Text="yesterday" Value="yesterday"></asp:ListItem>
           <asp:ListItem Text="last week" Value="lastweek"></asp:ListItem>
           <asp:ListItem Text="last month" Value="lastmonth"></asp:ListItem>
           <asp:ListItem Text="last year" Value="lastyear"></asp:ListItem>
        </asp:DropDownList><br />
        <asp:TextBox ID="TxtFromDate" runat="server" ></asp:TextBox>&nbsp<asp:TextBox ID="TxtToDate" runat="server" ></asp:TextBox>

代码隐藏:

 Protected Sub DdlTimeSpan_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim fromDate As Date = Date.Now
    Dim toDate As Date
    Select Case DirectCast(sender, DropDownList).SelectedValue
        Case "today"
            toDate = Date.Now
        Case "yesterday"
            toDate = Date.Now.AddDays(-1)
        Case "lastweek"
            toDate = Date.Now.AddDays(-7)
        Case "lastmonth"
            fromDate = Date.Now.AddMonths(-1)
        Case "lastyear"
            toDate = Date.Now.AddYears(-1)
    End Select
    Me.TxtFromDate.Text = fromDate.ToShortDateString
    Me.TxtToDate.Text = toDate.ToShortDateString
End Sub

C#:

protected void DdlTimeSpan_SelectedIndexChanged(object sender, System.EventArgs e)
{
    System.DateTime fromDate = System.DateTime.Now;
    System.DateTime toDate = default(System.DateTime);
    switch (((DropDownList)sender).SelectedValue) {
        case "today":
            toDate = System.DateTime.Now;
            break;
        case "yesterday":
            toDate = System.DateTime.Now.AddDays(-1);
            break;
        case "lastweek":
            toDate = System.DateTime.Now.AddDays(-7);
            break;
        case "lastmonth":
            fromDate = System.DateTime.Now.AddMonths(-1);
            break;
        case "lastyear":
            toDate = System.DateTime.Now.AddYears(-1);
            break;
    }
    this.TxtFromDate.Text = fromDate.ToShortDateString();
    this.TxtToDate.Text = toDate.ToShortDateString();
}