Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get short month name from full month name?

Tags:

c#

.net

datetime

I need to get the short month name for the full month name.

For example,

DateTime.Now.ToString("MMMM", CultureInfo.CurrentCulture);
this returns - "agosto"

DateTime.Now.ToString("MMM", CultureInfo.CurrentCulture);
this returns - "ago."

This two codes works only to get the current month. I need to get the short month name for all the months.

If I give "agosto", it should return "ago.". How could I achieve this?

like image 559
WP_Insane Avatar asked Oct 21 '25 23:10

WP_Insane


1 Answers

An alternative to Soner's answer that doesn't use a loop:

public static string GetAbbreviatedFromFullName(string fullname)
{
    DateTime month;
    return DateTime.TryParseExact(
            fullname,
            "MMMM",
            CultureInfo.CurrentCulture,
            DateTimeStyles.None,
            out month)
        ? month.ToString("MMM")
        : null;
}
like image 160
Rawling Avatar answered Oct 24 '25 13:10

Rawling