根据区域设置正确排序月份和日期
我想以人类可读的格式显示一周的开始和结束日期。使用 MMMM d
作为日期格式基础。例如:
- March 7 - 13
- February 28 - March 6
- 7-13 de marzo
因此,如果一周的开始和结束属于同一个月,我只想提及月份一次。我希望这在不同的地区都是准确的。例如,在西班牙语和白俄罗斯语中,日会在月之前,而在西班牙语中,月份名称之前会添加“de”。
我用来
DateFormatter.dateFormat(
fromTemplate: "MMMM d",
options: 0,
locale: Locale(identifier: Bundle.main.preferredLocalizations.first
获取正确的格式,然后尝试解析它并用两个数字替换天数,以表示一周完全落入同一个月。我意识到我的代码有点老套,可能无法保证适用于所有可能的区域设置。但我不知道是否有更简单/更好的方法。
这是我到目前为止所拥有的:
func weekDateTitle(firstDay: Date, lastDay: Date) -> String {
let dateTemplate = DateFormatter.dateFormat(
fromTemplate: "MMMM d",
options: 0,
locale: Locale(identifier: Bundle.main.preferredLocalizations.first ?? "en"))
let formatter = DateFormatter()
// If the week ends on the same month as it begins, we use short label format,
// like "Month X - Y"
if firstDay.isSameMonth(as: lastDay) {
formatter.dateFormat = "MMMM"
let month = formatter.string(from: firstDay)
formatter.dateFormat = "d"
let days = "\(formatter.string(from: firstDay)) - \(formatter.string(from: lastDay))"
return dateTemplate?
.replacingFirstOccurrence(of: "MMMM", with: month)
.replacingFirstOccurrence(of: "d", with: days)
.replacingOccurrences(of: "'", with: "") ?? ""
} else {
// Not really important
}
}
I want to display start and end date of week in a human readable format. Using MMMM d
as date format base. For example:
- March 7 - 13
- February 28 - March 6
- 7-13 de marzo
So if week start and end falls under the same month, I want to mention month only once. And I want this to be accurate in different locales. In Spanish and Belarussian for example day would go before month, and in Spanish there will be "de" added before the month name.
I'm using
DateFormatter.dateFormat(
fromTemplate: "MMMM d",
options: 0,
locale: Locale(identifier: Bundle.main.preferredLocalizations.first
to get proper format and then try to parse it and replace days with two numbers for when week completely falls into same month. I realized that code I have is a bit hacky and might not guarantee to work with all possible locales. But I have no idea is there easier / better way.
Here is what I have so far:
func weekDateTitle(firstDay: Date, lastDay: Date) -> String {
let dateTemplate = DateFormatter.dateFormat(
fromTemplate: "MMMM d",
options: 0,
locale: Locale(identifier: Bundle.main.preferredLocalizations.first ?? "en"))
let formatter = DateFormatter()
// If the week ends on the same month as it begins, we use short label format,
// like "Month X - Y"
if firstDay.isSameMonth(as: lastDay) {
formatter.dateFormat = "MMMM"
let month = formatter.string(from: firstDay)
formatter.dateFormat = "d"
let days = "\(formatter.string(from: firstDay)) - \(formatter.string(from: lastDay))"
return dateTemplate?
.replacingFirstOccurrence(of: "MMMM", with: month)
.replacingFirstOccurrence(of: "d", with: days)
.replacingOccurrences(of: "'", with: "") ?? ""
} else {
// Not really important
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑
DateIntervalFormatter
:对于同一个月,将生成“7 – 13 de marzo”和“3 月 7 – 13”。
对于不同的月份,“7 de febrero – 13 de marzo”和“February 7 – March 13”。
这些格式化程序存在奇怪的边缘情况(例如,
.long
的dateStyle
在英语区域设置中有效,但在其他区域设置中无效)。如果上述内容在您的所有目标区域设置中不能完美运行,我不会感到惊讶,因此您可能需要仔细检查。但是,根据我的经验,这些格式化程序可以让您非常接近。它似乎可以满足您对英语和西班牙语的需求......Consider
DateIntervalFormatter
:For the same month, that will generate “7 – 13 de marzo” and “March 7 – 13”.
For different months, “7 de febrero – 13 de marzo” and “February 7 – March 13”.
There are weird edge cases with these formatters (e.g., a
dateStyle
of.long
works in English speaking locales, but not in others). And I wouldn’t be surprised if the above was not working perfectly in all of your target locales, so you might want to double check. But, in my experience, these formatters can get you pretty close. It appears to achieve what you need in English and Spanish…