How to get number of months between 2 dates c#
class Program
{
static void Main(string[] args)
{
//First Date
DateTime firstDate = new DateTime(2017, 03, 03);
//Second Date
DateTime secondDate =new DateTime(2018, 06, 06); //DateTime.Now;
int months= MonthDiff(firstDate, secondDate);
Console.WriteLine("First Date :"+firstDate);
Console.WriteLine("Second Date :" + secondDate);
Console.WriteLine("Months :"+months);
Console.ReadLine();
}
public static int MonthDiff(DateTime d1, DateTime d2)
{
int m1;
int m2;
if(d1<d2)
{
m1 = (d2.Month - d1.Month);//for years
m2 = (d2.Year - d1.Year) * 12; //for months
}
else
{
m1 = (d1.Month - d2.Month);//for years
m2 = (d1.Year - d2.Year) * 12; //for months
}
return m1 + m2;
}
}
Also in C#:
- binding c#
- unity persistent data
- C# get all child classes of a class
- what is the or symbol in C#
- button color uwp c#
- c# webrequest cookies
- unity rotate vector
- unity oncollisionenter2d
- c# declare an int list
- how to set progress openedge driver name for odbc connection c#
- create object in c#
- bitmap to byte array c#
- ignore fakeiteasy
- c# how to open file explorer
- c# discord bot
- c# string methods
- read embedded resource c# xml
- iteration c#
- c# string remove special characters
- .net core cli
- Movement 2d unity
- get out of foreach statement c#
- unity c# foreach
- c# try catch error