Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have start date and end date in my website

how to calculate no of without count of days for example

Start date 30/5/2015
End date 1/6/2015

in this case also i want to show 2 months May-2015 and june-2015

my code is

it calculate total days between dates,

protected void txtStartDate_TextChanged(object sender, EventArgs e)
       string inputString = txtStartDate.Text;
       DateTime dt = DateTime.ParseExact(inputString, "yyyy/MM/dd", CultureInfo.InvariantCulture);
       dt = dt.AddMonths(Convert.ToInt32(txtNoOfMonths.Text));
       txtEndDate.Text = dt.ToString("yyyy/MM/dd");

       DateTime Date1 = Convert.ToDateTime(txtStartDate.Text);
       DateTime Date2 = Convert.ToDateTime(txtEndDate.Text);
       int DayDiff = (Date2.Date - Date1.Date).Days;
       Label1.Text = "Total days"+" "+(DayDiff.ToString());

how to calculate months
Updated 23-Apr-23 1:04am
Michael_Davies 30-May-15 5:49am    
Use DateDiff and add 1...

Dim date1, date2 As Date

date1 = "30/5/2105"
date2 = "1/6/2105"

Dim m As Integer

m = DateDiff(DateInterval.Month, date1, date2) + 1

Assuming the day of the month is irrelevant (i.e. the diff between 2011.1.1 and 2010.12.31 is 1), with date1 > date2 giving a positive value and date2 > date1 a negative value

((date1.Year - date2.Year) * 12) + date1.Month - date2.Month
Or, assuming you want an approximate number of 'average months' between the two dates, the following should work for all but very huge date differences.

date1.Subtract(date2).Days / (365.25 / 12)
Note, if you were to use the latter solution then your unit tests should state the widest date range for which your application is designed to work with and validate the results of the calculation accordingly.
Share this answer
Share this answer
You can use LINQ to find this consider the following code:

string StartDate="3/15/2015";
           string EndDate = "5/6/2015";
           DateTime ActualStartDate = DateTime.Parse(StartDate);
           DateTime ActualEndDate = DateTime.Parse(EndDate);
           ActualEndDate = new DateTime(ActualEndDate.Year, ActualEndDate.Month, DateTime.DaysInMonth(ActualEndDate.Year, ActualEndDate.Month));
           var diff = Enumerable.Range(0, Int32.MaxValue)
                                .Select(e => ActualStartDate.AddMonths(e))
                                .TakeWhile(e => e <= ActualEndDate)
                                .Select(e => e.ToString("MMMM"));

It gives you output as {"March","April","May"}
Share this answer
Please use the below function in your code and show months between two dates :-
private string getMonthsBetweenTwoDates(DateTime Date1, DateTime Date2)
        var diff = Enumerable.Range(0, Int32.MaxValue)
                     .Select(e1 => Date1.AddMonths(e1))
                     .TakeWhile(e1 => e1 <= Date2)
                     .Select(e1 => e1.ToString("MMM/yyyy")).ToList();

        return  string.Join(",", diff.ToArray()).ToString();
Share this answer
simply using this Query and get month difference between two dates.

Declare @Start DateTime
Declare @End DateTime

Set @Start = '5/30/2015'
Set @End = '6/1/2015'

Select DateDiff(Month, @Start, @End + 1)
Share this answer
DateTime fromDt = DateTime.Parse("18-12-2022");
                DateTime toDt = DateTime.Parse("10-02-2021");
                int fromMon = fromDt.Month;
                int fromYear = fromDt.Year;
                int toMon = toDt.Month;
                int toYear = toDt.Year;
                if (toMon < fromMon)
                    toMon += 12;
                int months = ((toYear - fromYear) * 12 + toMon - fromMon) + 1;
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900