find month number from date csharp
Code Example - find month number from date csharp
string name = DateTime.ParseExact("01/21/2014", "MM/dd/yyyy", null).ToString("MMMM"); //January
compute months csharp
public static int GetMonthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
csharp get month number from name
int month = DateTime.ParseExact(MonthNameStr, "MMMM", CultureInfo.CurrentCulture ).Month
getname of month from date csharp
using System;
using System.Globalization;
class Program
{
static void Main()
{
Console.WriteLine(DateTime.Now.ToMonthName());
Console.WriteLine(DateTime.Now.ToShortMonthName());
Console.Read();
}
}
static class DateTimeExtensions
{
public static string ToMonthName(this DateTime dateTime)
{
return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);
}
public static string ToShortMonthName(this DateTime dateTime)
{
return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);
}
}
csharp get month number
string sMonth = DateTime.Now.ToString("MM");
How to get an array of months in csharp
var months = new string[12];
for (var month = 1; month <= 12; month++)
{
var firstDay = new DateTime(DateTime.Now.Year, month, 1);
var name = firstDay.ToString("MMMM", CultureInfo.CreateSpecificCulture("en"));
months[month - 1] = name;
}
foreach (var month in months)
{
Console.WriteLine(%%%~COMPRESS~PRE~5~%%%quot;-> {month}");
}