I need to display today's date in the format November 22,2017
in C#
. How can I do this? Any suggestions. I know this way .ToString("mm/dd/yyyy")
. But this does not suit the current scenario. Thanks
Asked
Active
Viewed 160 times
-2

Vishvadeep singh
- 1,624
- 1
- 19
- 31

Sam Daniel
- 141
- 1
- 3
- 17
3 Answers
3
This should work:
DateTime.Now.ToString("MMMM dd,yyyy")
As Soner Gönül has mentioned, the month name is culture dependent. So it's better to specify the culture.
Invariant:
DateTime.Now.ToString("MMMM dd,yyyy", System.Globalization.CultureInfo.InvariantCulture)
or for german:
DateTime.Now.ToString("MMMM dd,yyyy", System.Globalization.CultureInfo.GetCultureInfo("de-DE"))

K. H.
- 208
- 1
- 5
-
Be careful about the culture here. If OP's `CurrentCulture` is _not_ based on english, this might generate month names other than english. It would be better to use english based culture like `InvariantCulture`. – Soner Gönül Nov 22 '17 at 07:39
0
DateTime
does not store display format.
Only string representations of DateTime have display format - you need to use .ToString("MMMM dd,yyyy")
.
However, note that month names will be effected by the CultureInfo
you are working with, so if you want the month name to be in a specific language, regardless of your current culture, you should use .ToString("MMMM dd,yyyy", new CultureInfo("en-US"))
(or any other language you want - "it-IT", "fr-FR" etc')

Zohar Peled
- 79,642
- 10
- 69
- 121
0
For short month names use:
string monthName = new DateTime(2010, 8, 1)
.ToString("MMM", CultureInfo.InvariantCulture);
For long/full month names for Spanish ("es") culture
string fullMonthName = new DateTime(2015, i, 1).ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));

Barr J
- 10,636
- 1
- 28
- 46