Get current month c# code example
Example 1: c# get month number
string sMonth = DateTime.Now.ToString("MM");
Example 2: getname of month from date c#
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);
}
}
Example 3: c# get current month number
System.DateTime moment = new System.DateTime(
1999, 1, 13, 3, 57, 32, 11);
int year = moment.Year;
int month = moment.Month;
int day = moment.Day;
int hour = moment.Hour;
int minute = moment.Minute;
int second = moment.Second;
int millisecond = moment.Millisecond;