How to Parse a Month Name (String) to an Integer for Comparison in C#

How to parse a month name (string) to an integer for comparison in C#?

DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month

Although, for your purposes, you'll probably be better off just creating a Dictionary<string, int> mapping the month's name to its value.

Convert a string containing monthName to Int of MonthDigit

int monthInDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;

Convert month string to int format

You can also use ParseExact method. i.e,

string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;

C# Get Month INT from Month String

int month = DateTime.ParseExact(MonthNameStr, "MMMM", CultureInfo.CurrentCulture ).Month

or you can do

int  month = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList().IndexOf(MonthNameStr) + 1;

Library for translating months into integers C# .NET

According to MSDN DateTimeFormatInfo.MonthNames, "march" is not valid, it has to be capitalized.

A one-dimensional array of type String containing the culture-specific full names of the months. In a 12-month calendar, the 13th element of the array is an empty string. The array for InvariantInfo contains "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", and "". [emphasis mine]

In powershell, you can get this list using:

[System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.MonthNames

Get Month name from month number

For short month names use:

string monthName = new DateTime(2010, 8, 1)
.ToString("MMM", CultureInfo.InvariantCulture);

For long/full month names for Spanish ("es") culture:

string fullMonthName = new DateTime(2015, i, 1).ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));

Converting string with month that is not integer to datetime

you can use:

 var date = DateTime.Parse("Mar 25 2013 6:30PM");

Console.WriteLine(date.ToString()); /*This will output the date in the required format */

How to display month number on selecting month name

Use this code to convert the selected Month Name into a Month Number

DateTime.ParseExact(monthName, "MMMM", CultureInfo.InvariantCulture).Month

Need String Padding?

PadleftMonthNumberString.PadLeft(2, "0")

References

  • How to parse a month name (string) to an integer for comparison in C#?
  • PadLeft
  • DateTime.ParseExact

Sample Console Application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string mnthname = "january";
int i = DateTime.ParseExact(mnthname, "MMMM", System.Globalization.CultureInfo.InvariantCulture).Month;
Console.WriteLine(i.ToString());
Console.ReadLine();
}
}
}

How to convert in c# date if date month is text

You can use MMMM d, yyyy format with an english-based culture like InvariantCulture;

string s = "December 6, 2011";
DateTime dt;
if(DateTime.TryParseExact(s, "MMMM d, yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd")); // 2011-12-06
}

"MMMM" format specifier represents the full name of the month based on culture settings.



Related Topics



Leave a reply



Submit