gpt4 book ai didi

c# - 如何使用 CultureInfo 获取 12 小时或 24 小时时间(不带日期)

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:24 25 4
gpt4 key购买 nike

我想使用 C# 以适当的文化格式格式化一天中的时间。

例如,如果当前文化是 en-US,我想显示 1:00 PM,如果当前文化是 fr-FR,我想显示 13:00。我只想要一天中的时间,我不想要日期。

//timeOfDay is a DateTime object.

//This will return the 12 hour clock regardless of culture:
time = timeOfDay.ToString("h:mm tt", CultureInfo.CurrentCulture);
//This will return the 24 hour clock regardless of culture
time = timeOfDay.ToString("H:mm tt", CultureInfo.CurrentCulture);
//This will return the correct clock for the culture, but the date will also be present
time = timeOfDay.ToString(CultureInfo.CurrentCulture);

请注意,“tt”代表上午/下午,它在文化上是敏感的(在法国它应该是空白的)。

如何在没有日期的情况下获取适合当前文化的时钟格式?

最佳答案

这似乎可行:

string time = timeOfDay.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, CultureInfo.CurrentCulture);

第二个参数可能也是不必要的。

关于c# - 如何使用 CultureInfo 获取 12 小时或 24 小时时间(不带日期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639434/

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