如何从完整的月份名称获取短月份名称?

时间:2014-08-21 06:57:04

标签: c# .net datetime

我需要获取完整月份名称的短月份名称。

例如,

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

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

这两个代码仅用于获取当前月份。我需要获得所有月份的短月名称。

如果我提供"agosto",则应返回"ago."。我怎么能做到这一点?

11 个答案:

答案 0 :(得分:4)

Soneranswer的替代方案,不使用循环:

public static string GetAbbreviatedFromFullName(string fullname)
{
    DateTime month;
    return DateTime.TryParseExact(
            fullname,
            "MMMM",
            CultureInfo.CurrentCulture,
            DateTimeStyles.None,
            out month)
        ? month.ToString("MMM")
        : null;
}

答案 1 :(得分:3)

试试这种方式

var culture = CultureInfo.GetCultureInfo("en-US");
var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);

foreach (string name in dateTimeInfo.AbbreviatedMonthNames)
{
    Console.WriteLine(name);
}

答案 2 :(得分:3)

  

如果我提供" agosto",它应该返回"前。"

然后你可以使用DateTimeFormatInfo.MonthNames之类的;

public static string GetAbbreviatedFromFullName(string fullname)
{
    string[] names = DateTimeFormatInfo.CurrentInfo.MonthNames;
    foreach (var item in names)
    {
        if (item == fullname)
        {
            return DateTime.ParseExact(item, "MMMM", CultureInfo.CurrentCulture)
                                 .ToString("MMM");
        }
    }
    return string.Empty;
}

然后可以像GetAbbreviatedFromFullName("agosto");

一样调用它

答案 3 :(得分:1)

如果我理解你的问题,你想传递一个月份的全名函数,并获得月份的简称?

此方法假定传递的MonthLongName是当前区域性中的有效月份名称。 如果不是,它将抛出异常。如果有可能,请进行一些验证。

public String GetMonthShortName(String MonthLongName)
{
    return DateTime.ParseExact(MonthLongName, "MMMM", CultureInfo.CurrentCulture ).ToString("MMM", CultureInfo.CurrentCulture);
}

this answer

借来的一些代码

答案 4 :(得分:1)

如果可以获取月份号,则通过这种方法很容易获得缩写名称或全名。

缩写月份名称:

 CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(DateTime.Today.Month)

全月名称:

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Today.Month)

答案 5 :(得分:0)

如果您想要所有月份名称,可以使用以下内容:

for (int i = 0; i < 12; i++)
{
    Console.WriteLine(DateTime.Now.AddMonths(i).ToString("MMM"));
}

答案 6 :(得分:0)

CultureInfo已经有表示完整和缩写月份名称的数组:

CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[
    Array.FindIndex( CultureInfo.CurrentCulture.DateTimeFormat.MonthNames,
                     t => t.Equals("agosto", StringComparison.CurrentCultureIgnoreCase))];

答案 7 :(得分:0)

我遇到了同样的问题,我想添加我的解决方案(基于@Rawling的回答)以确保完整性。

由于我需要在XAML中进行转换,所以我编写了以下转换器。

由于我不想在资源上添加数月和数天,因此转换从Invariant long(我在&#34;标签&#34; XAML元素中)转换为短流媒体文化(需要在&#34;内容&#34;),从而节省额外的翻译

我还在工作日创建了一个同样的转换器。

using System;
using System.Globalization;
using System.Windows.Data;

namespace Rittal.RiZone.GUI.ValueConverters
{
    public class LongToShortMonthConverter : IValueConverter
    {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value">Long month name from InvariantCulture</param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns>Short text representation of the month in CurrentCulture; null in case no match was found</returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        DateTime month;
        return DateTime.TryParseExact(
            value.ToString(),
            "MMMM",
            CultureInfo.InvariantCulture,
            DateTimeStyles.None,
            out month)
            ? month.ToString("MMM", CultureInfo.CurrentCulture)
            : null;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value">Short month name from CurrentCulture</param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns>Long text representation of the month in InvariantCulture; null in case no match was found</returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        DateTime month;
        return DateTime.TryParseExact(
            value.ToString(),
            "MMM",
            CultureInfo.CurrentCulture,
            DateTimeStyles.None,
            out month)
            ? month.ToString("MMMM", CultureInfo.InvariantCulture)
            : null;
    }
}

}

答案 8 :(得分:0)

DateTime.Now.ToString(&#34; dd MMM yyyy&#34;); 这会返回 - &#34; agosto&#34;;

DateTime.Now.ToString(&#34; dd MMM yyyy&#34;); 这将返回 - &#34;前。&#34 ;;

答案 9 :(得分:0)

这是您需要的:

var dateUtil = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentCulture);
return dateUtil.AbbreviatedMonthNames[DateTime.Now.Month -1];

答案 10 :(得分:-1)

CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
DateTimeFormatInfo abrv = ci.DateTimeFormat;

DateTime dat = new DateTime(2014, 1, 1);

for (int ctr = 0; ctr < abrv.Calendar.GetMonthsInYear(dat.Year); ctr++) {
     Response.Write(dat.AddMonths(ctr).ToString("MMM", abrv) + "<br />");
}