datetime.tostring 月份和日期语言
我有一个不同国籍的人的电子邮件地址列表(对于每个人,我都有 ISO 代码),
当我向所有这些人发送电子邮件时,在邮件的文本中,我需要将日期时间字段转换为字符串格式化在他们的特定文化中。
为此,我正在做
CultureInfo ci = new CultureInfo(ISO);
myStringDate = myDate.ToString(ci.DateTimeFormat.ShortDatePattern);
并且工作完美,但如果我使用 LongDatePattern 而不是短,用于显示像“星期一,2010 年 6 月 13 日”这样的日期,除了日期和月份的语言之外,它的工作正常。
如果人员文化是 IT,我需要显示“Martedi”和“Giugno”而不是“星期一”和“六月”,
我怎样才能在不改变当前 UI 文化的情况下做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些模式描述了
DateTime
输出结果中的年、月、日和其他参数位置。但月份和日期的名称取自CultureInfo
对象,而不是模式。有DateTime.ToString()
重载 支持传递CultureInfo
参数以及格式。这样,
.ToString()
将尊重指定区域性的模式和名称Those patterns describe year, month, day and other parameter locations in the output result of
DateTime
. But month and day names are taken from theCultureInfo
object, not pattern. There's theDateTime.ToString()
overload that supports passing theCultureInfo
parameter along with the format.This way,
.ToString()
will respect both pattern and names from specified culture您仅指定了格式模式,因此它采用默认格式中的其他设置。您还应该指定格式提供程序:
或使用标准格式字符串
D
来表示长日期模式(因为它将从您指定的格式提供程序中获取实际模式):演示:
输出:
You have only specified the format pattern, so it takes the other settings from the default format. You should also specify the format provider:
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):Demo:
Output:
我刚刚遇到了类似的问题并解决了它:
这要归功于 此链接。
I just had a similar problem and solved it with:
That was thanks to this link.
使用 DateTimeFormatInfo 类。
Use
DateTimeFormatInfo
class.DateTime.ToString()
还有另一个重载,您可以使用它指定所需的模式(如您当前的和要使用的区域性。尝试: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: