Click here to Skip to main content
Click here to Skip to main content

Splitting Dates of Different Months in Asp.Net

, 27 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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
<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:

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:

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)

About the Author

Charan_Kumar
Software Developer
India India
No Biography provided

Comments and Discussions

 
SuggestionHow about using LINQ? [modified] PinmemberJon Clare27-Feb-14 4:17 
GeneralRe: How about using LINQ? PinmemberGeorge Swan27-Feb-14 22:33 
GeneralRe: How about using LINQ? PinprofessionalRichard Deeming7-Mar-14 2:02 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Feb 2014
Article Copyright 2014 by Charan_Kumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid