以"Apr"格式获取当前月份 [英] Getting current month in format 'Apr'

查看:113
本文介绍了以"Apr"格式获取当前月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int month;
month = System.DateTime.Now.Month;


我正在尝试以``Apr''格式获取当前月份值,但只能以数字格式获取.我正在使用Web应用程序.尝试使用ToString


问候,
Madhu


I am trying to get the current month value in the format ''Apr'' but have been able to get in number format only.I am working on a web application.Tried using ToString


Regards,
Madhu

推荐答案

尝试一下,
Try this,
string strMonth = System.DateTime.Now.ToString("MMM");


这个答案很简单,应该很明显.有两种方法,一种使用自定义格式,另一种更易于编辑的解决方案.

1)
This answer is simple and should be obvious. There are two methods of which one uses custom formats and another more easily editable solution.

1)
string Month = System.DateTime.Now.ToString("MMM");



2)创建一个包含所有月份名称的字符串数组,然后只需使用System.DateTime.Now.Month对其进行索引即可获取所需的月份字符串.您可能要检查一月是0还是1,以及在索引到数组之前后者是否减去1.



2) Create a string array with all the names of the months and then simply index into it with System.DateTime.Now.Month to get month string you wanted. You might want to check whether January comes out as 0 or 1 and if the latter subtract 1 before indexing into the array.


string srrMonth= system.dateTime.Now.ToString("MMM");


这篇关于以"Apr"格式获取当前月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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