15,966,543 members
See more:
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,

C#
```protected void txtStartDate_TextChanged(object sender, EventArgs e)
{
string inputString = txtStartDate.Text;
DateTime dt = DateTime.ParseExact(inputString, "yyyy/MM/dd", CultureInfo.InvariantCulture);
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
Posted
Updated 23-Apr-23 1:04am
v2
Michael_Davies 30-May-15 5:49am

Dim date1, date2 As Date

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

Dim m As Integer

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

## Solution 1

SQL
```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.```

## Solution 3

You can use LINQ to find this consider the following code:

C#
```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)
.TakeWhile(e => e <= ActualEndDate)
.Select(e => e.ToString("MMMM"));
```

It gives you output as {"March","April","May"}

## Solution 4

Please use the below function in your code and show months between two dates :-
C#
```private string getMonthsBetweenTwoDates(DateTime Date1, DateTime Date2)
{
var diff = Enumerable.Range(0, Int32.MaxValue)
.TakeWhile(e1 => e1 <= Date2)
.Select(e1 => e1.ToString("MMM/yyyy")).ToList();

return  string.Join(",", diff.ToArray()).ToString();
}```

v2

## Solution 5

simply using this Query and get month difference between two dates.

SQL
```Declare @Start DateTime
Declare @End DateTime

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

Select DateDiff(Month, @Start, @End + 1)```

## Solution 6

```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;
toYear--;
}
int months = ((toYear - fromYear) * 12 + toMon - fromMon) + 1;```