c# - 如何从全月名中获取短月名?

标签 c# .net datetime

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

例如,

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

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

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

如果我输入 "agosto",它应该返回 "ago."。我怎样才能做到这一点?

最佳答案

Soner 的替代品的 answer不使用循环:

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

关于c# - 如何从全月名中获取短月名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25420045/

相关文章:

c# - .NET 解决方案 - 许多项目与一个项目

c# - 合并两个不同类型的列表

c# - 如何从 .NET 中的 x 行读取文件

C#:CommandLineParser 如何解析URL?

c# - List<>.Exists() 在 WP7 中不存在?

javascript - 在 Express 中显示每个请求的日志时间?

c# - 无法将类型为 'MongoDB.Bson.Serialization.Serializers.DateTimeSerializer' 的对象转换为类型 'MongoDB.Bson.Serialization.IBsonSerializer`

python - 将日期转换为小时

sql - 为什么这个简单的 SQL 语句不起作用?

c# - 我可以在读取过程中中断数据读取器吗?