Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#
Tip/Trick

Splitting Dates of Different Months in Asp.Net

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
27 Feb 2014CPOL 19.7K   6   3
Splitting dates of different months in Asp.Net

Introduction 

Sometimes there may be necessary of splitting days in a particular month. As it is easy if we know the month of the date. What if the selection of dates are of different months? then look over my trick.

Background

If you want require inserting(records) dates of particular month to database or need to show, splitting of dates by month is useful.

Using the code

Here for viewers convenient i am binding the separated dates to GridView with number of days
HTML
<asp:GridView ID="gvDates" runat="server" AutoGenerateColumns="false">
  <Columns>
      <asp:BoundField DataField="startDate" HeaderText="Start Date" dataformatstring="{0:dd/MM/yyyy}"/>
      <asp:BoundField DataField="endDate" HeaderText="End Date" dataformatstring="{0:dd/MM/yyyy}" />
      <asp:BoundField DataField="days" HeaderText="Days" />
  </Columns>
</asp:GridView>     

For List, I am defining class:

C#
public class splitDates
{
   public DateTime startDate { get; set; }
   public DateTime endDate { get; set; }
   public double days { get; set; }
}

Here on button click, I am splitting the dates:

C#
protected void btnSplit_Click(object sender, EventArgs e)
{
    DateTime dt1, dt2, monthStartDate, monthEndDate;
    List<splitDates> listDates = new List<splitDates>();
    dt1 = Convert.ToDateTime(txtFromDate.Text);
    dt2 = Convert.ToDateTime(txtToDate.Text);
    monthStartDate = dt1.AddDays(-1 * dt1.Day + 1);
    monthEndDate = monthStartDate.AddMonths(1).AddDays(-1);
    
    if (monthEndDate < dt2)
    {
        while (monthEndDate < dt2)
        {
            listDates.Add(new splitDates { startDate = dt1, endDate = monthEndDate, days = ((monthEndDate - dt1).TotalDays) + 1 });
            monthStartDate = monthEndDate.AddDays(1);
            dt1 = monthStartDate;
            monthEndDate = monthStartDate.AddMonths(1).AddDays(-1);
        }

        listDates.Add(new splitDates { startDate = monthStartDate, endDate = dt2, days = ((dt2 - monthStartDate).TotalDays) + 1 });
    }
    else
    {
        listDates.Add(new splitDates { startDate = dt1, endDate = dt2, days = (dt2 - dt1).Days + 1 });
    }

    gvDates.DataSource = listDates;
    gvDates.DataBind();
}
   

Resources  

  1. www.stackoverflow.com
  2. www.nullskull.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionHow about using LINQ? Pin
Jon Clare27-Feb-14 4:17
Jon Clare27-Feb-14 4:17 
GeneralRe: How about using LINQ? Pin
George Swan27-Feb-14 22:33
mveGeorge Swan27-Feb-14 22:33 
GeneralRe: How about using LINQ? Pin
Richard Deeming7-Mar-14 2:02
mveRichard Deeming7-Mar-14 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.