datetime.tostring month and day language
Asked Answered
F

5

19

i have a list of email addresses of people that have different nationalities (for each person i have the iso code)

when i send the email to all these people, in the text of the mail i need to to convert a datetime field to a string formatted in their specific culture.

for this i'm doing

CultureInfo ci = new CultureInfo(ISO);
myStringDate = myDate.ToString(ci.DateTimeFormat.ShortDatePattern);

and work perfect, but if i use LongDatePattern instead short, for displaying date like "Monday, 13 June 2010" its work fine except the language of the day and month.

if the person culture is it-IT i need to display "Martedi" and "Giugno" not "monday" and "June"

how can i do that without change the current UI culture?

Florescence answered 26/1, 2012 at 17:4 Comment(0)
S
25

Those patterns describe year, month, day and other parameter locations in the output result of DateTime. But month and day names are taken from the CultureInfo object, not pattern. There's the DateTime.ToString() overload that supports passing the CultureInfo parameter along with the format.

CultureInfo culture = new CultureInfo(ISO); 
DateTime.Now.ToString(culture.DateTimeFormat.LongDatePattern, culture);

This way, .ToString() will respect both pattern and names from specified culture

Springclean answered 26/1, 2012 at 17:18 Comment(0)
M
13

You have only specified the format pattern, so it takes the other settings from the default format. You should also specify the format provider:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern, ci);

or using the standard format string D for long date pattern (as it will take the actual pattern from the format provider that you specify):

myStringDate = myDate.ToString("D", ci);

Demo:

CultureInfo ci = new CultureInfo("it-IT");
Console.WriteLine(DateTime.Now.ToString("D", ci));
ci = new CultureInfo("sv-SE");
Console.WriteLine(DateTime.Now.ToString("D", ci));

Output:

giovedì 26 gennaio 2012
den 26 januari 2012
Mineralogist answered 26/1, 2012 at 17:15 Comment(0)
D
7

I just had a similar problem and solved it with:

DateTime.Now.ToString("dddd, dd MMMM yyyy", new System.Globalization.CultureInfo("it-IT"));

That was thanks to this link.

Dehlia answered 17/11, 2015 at 16:13 Comment(1)
simplest solution imo tyPee
C
4

Use DateTimeFormatInfo class.

string code = "mk-MK";
DateTimeFormatInfo info =
    DateTimeFormatInfo.GetInstance(CultureInfo.GetCultureInfo(code));
string longDate = 
    DateTime.Now.ToString(info.LongDatePattern, new System.Globalization.CultureInfo(code));
Cassaundra answered 26/1, 2012 at 17:11 Comment(0)
M
2

DateTime.ToString() has yet another overload with which you can specify both the pattern you want (as you are currently and the culture to use. Try:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern,
                               ci);
Minestrone answered 26/1, 2012 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.