DateFormatter本地化字符串——仅限日期和月份,没有年份[重复]

时间:2021-10-07 21:26:06

This question already has an answer here:

这个问题已经有了答案:

Is it possible to get localized string from date only with month and day?

是否可能只从日期到月和日获取本地化字符串?

let localizedDate = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .none)

This prints Sept 15, 2017 or 15 Sep 2017.

这是2017年9月15日或2017年9月15日的照片。

I would like to honor user's date format and not display the year, only month and day. I mean Sept 15 or 15 Sep. Is it possible?

我想尊重用户的日期格式,不显示年份,只显示月份和日期。我是说9月15日或9月15日,可能吗?

1 个解决方案

#1


1  

You can create a formate for just the day and the month and use that to create a format template. The DateFormatter will then localise it. As an example from an (Xcode9) playground:

您可以只创建一天和一个月的格式,并使用它创建一个格式模板。DateFormatter然后将其本地化。作为(Xcode9)游乐场的一个例子:

let date = Date()
let customFormat = "ddMMMM"

let gbLocale = Locale(identifier: "en_GB")
let ukFormat = DateFormatter.dateFormat(fromTemplate: customFormat, options: 0, locale: gbLocale)
let usLocale = Locale(identifier: "en_US")
let usFormat = DateFormatter.dateFormat(fromTemplate: customFormat, options: 0, locale: usLocale)

let formatter = DateFormatter()
formatter.dateFormat = ukFormat
formatter.string(from: date) // -> "09 September"

formatter.dateFormat = usFormat
formatter.string(from: date) // -> "September 09"

If you pass in Locale.current to the templateFormatter instead of the example ones I created, then you will get the localised versions of your template.

如果您传入Locale。当前到templateFormatter,而不是我创建的示例,然后您将获得模板的本地化版本。

Your question asks about short month format so you can replace "MMMM" in the custom format with "MMM" to get the shortened month name displayed instead of the full one. and a "d" instead of "dd" to not have leading "0" in the date. I'm sure you know what I mean.

你的问题是关于短月格式的,所以你可以用“MMM”替换自定义格式中的“MMMM”,以显示缩短的月份名称,而不是完整的月份名称。用“d”而不是“dd”,表示日期前没有“0”。我肯定你明白我的意思。

#1


1  

You can create a formate for just the day and the month and use that to create a format template. The DateFormatter will then localise it. As an example from an (Xcode9) playground:

您可以只创建一天和一个月的格式,并使用它创建一个格式模板。DateFormatter然后将其本地化。作为(Xcode9)游乐场的一个例子:

let date = Date()
let customFormat = "ddMMMM"

let gbLocale = Locale(identifier: "en_GB")
let ukFormat = DateFormatter.dateFormat(fromTemplate: customFormat, options: 0, locale: gbLocale)
let usLocale = Locale(identifier: "en_US")
let usFormat = DateFormatter.dateFormat(fromTemplate: customFormat, options: 0, locale: usLocale)

let formatter = DateFormatter()
formatter.dateFormat = ukFormat
formatter.string(from: date) // -> "09 September"

formatter.dateFormat = usFormat
formatter.string(from: date) // -> "September 09"

If you pass in Locale.current to the templateFormatter instead of the example ones I created, then you will get the localised versions of your template.

如果您传入Locale。当前到templateFormatter,而不是我创建的示例,然后您将获得模板的本地化版本。

Your question asks about short month format so you can replace "MMMM" in the custom format with "MMM" to get the shortened month name displayed instead of the full one. and a "d" instead of "dd" to not have leading "0" in the date. I'm sure you know what I mean.

你的问题是关于短月格式的,所以你可以用“MMM”替换自定义格式中的“MMMM”,以显示缩短的月份名称,而不是完整的月份名称。用“d”而不是“dd”,表示日期前没有“0”。我肯定你明白我的意思。