"get list of months and year between two dates c#" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "get list of months and year between two dates c#" answered properly. Developers are finding an appropriate answer about get list of months and year between two dates c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like get list of months and year between two dates c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on get list of months and year between two dates c#.
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;
}
}
Source: www.csharp-console-examples.com
total months between two dates c#
((date1.Year - date2.Year) * 12) + date1.Month - date2.Month
Source: stackoverflow.com
get list of months and year between two dates c#
public static IEnumerable<(string Month, int Year)> MonthsBetween(
DateTime startDate,
DateTime endDate)
{
DateTime iterator;
DateTime limit;
if (endDate > startDate)
{
iterator = new DateTime(startDate.Year, startDate.Month, 1);
limit = endDate;
}
else
{
iterator = new DateTime(endDate.Year, endDate.Month, 1);
limit = startDate;
}
var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
while (iterator <= limit)
{
yield return (
dateTimeFormat.GetMonthName(iterator.Month),
iterator.Year
);
iterator = iterator.AddMonths(1);
}
}
Source: stackoverflow.com
All those coders who are working on the C# based application and are stuck on get list of months and year between two dates c# can get a collection of related answers to their query. Programmers need to enter their query on get list of months and year between two dates c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about get list of months and year between two dates c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of get list of months and year between two dates c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "get list of months and year between two dates c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like get list of months and year between two dates c# resolved professionally and stay updated to the latest C# updates.