gpt4 book ai didi

c# - 如何将日期格式化为本地化的 Short MonthDay 字符串

转载 作者:太空狗 更新时间:2023-10-29 23:59:28 27 4
gpt4 key购买 nike

我想将 DateTime 格式化为一个字符串,其中包含缩写的月份名称和在图表中的轴标签中使用的日期。

默认的 DateTime 格式字符串不包含缩写的月份。我想没有标准,但我可以采用月份名称的前 3 个字符的子字符串并将其替换为 MonthDay 格式。我会使用 MonthDay 的原因是月份和日期的顺序取决于语言环境。

有没有人有更好的主意?

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#MonthDay

最佳答案

您可以采用 MonthDay 模式并将“MMMM”替换为“MMM”——然后应用该模式:

string pattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
pattern = pattern.Replace("MMMM", "MMM");
string formatted = dateTime.ToString(pattern);

这有点粗糙,但我相信它会起作用。

关于c# - 如何将日期格式化为本地化的 Short MonthDay 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2787235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com