Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display dynamically the previous month name

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);
like image 502
Shalni Avatar asked Oct 25 '10 12:10

Shalni


1 Answers

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.

like image 148
Fredrik Mörk Avatar answered Oct 20 '22 17:10

Fredrik Mörk