Month Name in c# (August)

S

sck10

Hello,

What I am trying to do is get the string(August) from:
hdnMonth.Value = DateTime.Now.Month.ToString();

Does c# have a function like the vb function "MonthName" ?
hdnMonth.Value = CType(MonthName(Month(Now())), String)

Thanks, sck10
 
L

Laurent Bugnion

Hi,
Hello,

What I am trying to do is get the string(August) from:
hdnMonth.Value = DateTime.Now.Month.ToString();

Does c# have a function like the vb function "MonthName" ?
hdnMonth.Value = CType(MonthName(Month(Now())), String)

Thanks, sck10

You don't say in which culture, so I am going to assume you want it in
the current culture:

string monthName
= CultureInfo.CurrentCulture.DateTimeFormat.GetMonth(
DateTime.Now.Month );

Note that with GetMonth, 1 returns January and not 0 like you would have
expected.

Note also that GetMonth can accept 1 to 13, with 13 returning an empty
string.

http://msdn.microsoft.com/library/d...ndatetimeformatinfoclassgetmonthnametopic.asp

HTH,
Laurent
 
M

Mark Rae

What I am trying to do is get the string(August) from:
hdnMonth.Value = DateTime.Now.Month.ToString();

Does c# have a function like the vb function "MonthName" ?
hdnMonth.Value = CType(MonthName(Month(Now())), String)

Easy as pie...

string strMonth = DateTime.Now.ToString("MMMM");
 
S

Steven Cheng[MSFT]

Thanks for Laurent and Mark's informative input.

Hi Steve,

As Mark has mentioned, as simple way to get the long month name of the date
is use the DateTime object ToString method and supply the "MMMM" format
string. Also, the below statement will only return the month name
associated with the current thread's CultureInfo

Response.Write("<br/>" + date.ToString("MMMM"));

if you want to return the culture-neutral (english) month name, you can
also supply the "InvariantCulture" formatter info.

Response.Write("<br/>" + date.ToString("MMMM",
CultureInfo.InvariantCulture));

Here are some useful info about common date/time formatting in .net
framework:

#Custom DateTime Format Strings
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcustomdatetimeform
atstrings.asp?frame=true

#Standard DateTime Format Strings
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconstandarddatetimefo
rmatstrings.asp?frame=true

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Sep 17, 2009
Messages
3
Reaction score
0
CultureInfo.CurrentCulture.DateTimeFormat.MonthNam es.GetValue(InvoiceDate.Month )

this code works for me. Thanks. CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(InvoiceDate.Month ).ToString()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top