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开发者_StackOverflow 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);
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.
Just to add in the the answer of @Fredrik Mörk, You can specify the Format without calling ToString()
method. i.e besides doing this:
yourDate.ToString("MMMM")
you can also do:
yourDate:MMMM
Usage:
$"Balance Up to {DateTime.Now.AddMonths(-1):MMMM}"
精彩评论