将月份 int 转换为月份名称 [英] Convert month int to month name

查看:28
本文介绍了将月份 int 转换为月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试使用 DateTime 结构将 1 到 12 之间的整数转换为缩写的月份名称.

I was simply trying to use the DateTime structure to transform an integer between 1 and 12 into an abbrieviated month name.

这是我尝试过的:

DateTime getMonth = DateTime.ParseExact(Month.ToString(), 
                       "M", CultureInfo.CurrentCulture);
return getMonth.ToString("MMM");

但是我在第一行得到一个 FormatException,因为该字符串不是一个有效的 DateTime.谁能告诉我怎么做?

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?

推荐答案

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);

请参阅此处了解更多详情.

DateTime dt = DateTime.Now;
Console.WriteLine( dt.ToString( "MMMM" ) );

或者,如果您想获得特定于文化的缩写名称.

Or if you want to get the culture-specific abbreviated name.

GetAbbreviatedMonthName(1);

参考

这篇关于将月份 int 转换为月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆