views:

46

answers:

1

I want to display the previous month name. My code is given below but it displays the index of that month. I want the name of that month. According to this given code it dipslays the tool tip as "Balance up to 9", but I want to display "Balance up to September". How get the name of that month?

lblPreviousBalance.ToolTip = "Balance up to " + (DateTime.Now.Month - 1);
+5  A: 

The following should work for you:

string previousMonth = DateTime.Now.AddMonths(-1).ToString("MMMM");

If you want it in a specific language, you can pass a CultureInfo object to the method:

string prevMonthInFrench = DateTime.Now.AddMonths(-1).ToString("MMMM", CultureInfo.GetCultureInfo("fr-FR"));

For more options you can check the Custom Date and Time Format Strings article at MSDN.

Fredrik Mörk
@Fredrik Mörk: what is CultureInfo. Is it variable name or something else. If i use the above code then it gives error as CultureInfo does not exit in current context.
Shalni
@Shalni: add `using System.Globalization;` to the using directives at the top of your file. In short `CultureInfo` is a class representing a language and region. More info here: [CultureInfo](http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx)
Fredrik Mörk
@Fredrik Mörk: Thanks sir
Shalni