Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

c# - datetime.tostring month and day language

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...