Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example below shows how to translate date period strings to date ranges based on the value selected from a DropDownList.

ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>Date Periods</title>
</head>
<body>
    <form id="form2" runat="server">
        <asp:DropDownList ID="DropDownList2" runat="server"  
                          AutoPostBack ="true" 
                          AppendDataBoundItems="true"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="-1">Select Period</asp:ListItem>
            <asp:ListItem Value="0">Today</asp:ListItem>
            <asp:ListItem Value="1">Yesterday</asp:ListItem>
            <asp:ListItem Value="2">This Week</asp:ListItem>
            <asp:ListItem Value="3">Last Week</asp:ListItem>
            <asp:ListItem Value="4">This Month</asp:ListItem>
        </asp:DropDownList>
    </form>
</body>
</html>


CODE BEHIND:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Value != "-1")
        {
            int period = int.Parse(DropDownList1.SelectedItem.Value);
            string parsePeriod = GetPeriods(ConvertDatePeriods(period));
            //print the converted period
            Response.Write(parsePeriod);
        }
    }

    public enum DatePeriods
    {
        Today,
        Yesterday,
        ThisWeek,
        LastWeek,
        ThisMonth
    }

    public DatePeriods ConvertDatePeriods(int intRepPeriod)
    {
        switch (intRepPeriod)
        {
            case 1:
                return DatePeriods.Yesterday;
            case 2:
                return DatePeriods.ThisWeek;
            case 3:
                return DatePeriods.LastWeek;
            case 4:
                return DatePeriods.ThisMonth;
        }
        return DatePeriods.Today;
    }

    public string GetPeriods(DatePeriods periods)
    {

        DateTime referenceDate;
        DateTime startDate = new DateTime();
        DateTime endDate = new DateTime();
        string dateString = string.Empty;

        switch (periods)
        {
            case DatePeriods.Today:
                referenceDate = DateTime.Now;
                dateString = referenceDate.ToShortDateString();
                break;
            case DatePeriods.Yesterday:
                referenceDate = DateTime.Now.AddDays(-1);
                dateString = referenceDate.ToShortDateString();
                break;
            case DatePeriods.ThisWeek:
                referenceDate = DateTime.Now;
                startDate = referenceDate;
                endDate = referenceDate.AddDays(6);
                dateString = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                break;
            case DatePeriods.LastWeek:
                referenceDate = DateTime.Now.AddDays(-7);
                startDate = referenceDate;
                endDate = startDate.AddDays(6);
                dateString = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                break;
            case DatePeriods.ThisMonth:
                referenceDate = DateTime.Now;
                startDate = new DateTime(referenceDate.Year, referenceDate.Month, 1);
                endDate = startDate.AddMonths(1).AddDays(-1);
                dateString = startDate.ToShortDateString() + " - " + endDate.ToShortDateString();
                break;
        }
        return dateString;
    }


The code above will return the corresponding date ranges based on the period selected from the DropDownList.

 

Technorati Tags: ,
Posted on Tuesday, April 14, 2009 3:52 AM ASP.NET , C# | Back to top


Comments on this post: Filtering Dates by Periods like (Today, Yesterday,This Week, Last Week, This Month)

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net