Click here to Skip to main content
13,298,451 members (64,154 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 26 Feb 2014

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


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.


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">
      <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" />

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 });
        listDates.Add(new splitDates { startDate = dt1, endDate = dt2, days = (dt2 - dt1).Days + 1 });

    gvDates.DataSource = listDates;




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


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

SuggestionHow about using LINQ? Pin
Jon Clare27-Feb-14 5:17
memberJon Clare27-Feb-14 5:17 
GeneralRe: How about using LINQ? Pin
George Swan27-Feb-14 23:33
memberGeorge Swan27-Feb-14 23:33 
GeneralRe: How about using LINQ? Pin
Richard Deeming7-Mar-14 3:02
professionalRichard Deeming7-Mar-14 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 27 Feb 2014
Article Copyright 2014 by Charan_Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid