I was simply trying to use the DateTime
structure to transform an integer between 1 and 12 into an abbrieviated month name.
Here is what I tried:
DateTime getMonth = DateTime.ParseExact(Month.ToString(), "M", CultureInfo.CurrentCulture); return getMonth.ToString("MMM");
However I get a FormatException
on the first line because the string is not a valid DateTime
. Can anyone tell me how to do this?
In MySQL, we can use a combination of functions 'MONTHNAME' and 'STR_TO_DATE' functions to get a month name from a month number. SQL SERVER: In SQL SERVER, we can use a combination of functions 'DATENAME' and 'DATEADD' functions to get a month name from a month number.
To get the month name from a number in Python, the easiest way is with strftime() and passing “%M”. You can also use the calendar module and the month_name() function.
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);
See Here for more details.
Or
DateTime dt = DateTime.Now; Console.WriteLine( dt.ToString( "MMMM" ) );
Or if you want to get the culture-specific abbreviated name.
GetAbbreviatedMonthName(1);
Reference
var monthIndex = 1; return month = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(monthIndex);
You can try this one as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With