如何将星期几从英语更改为语言环境
How to change day of week from English to Locale
我正在使用此 pod https://github.com/miraan/CalendarDateRangePickerViewController 处理日历。我更改了月份名称,但无法更改星期几。
我尝试添加 components.calendar?.locale = Locale(identifier: "ru_RU") 和 dateFormatter.locale = Locale.autoupdatingCurrent 但没有任何改变。
func getWeekdayLabel(weekday: Int) -> String {
var components = DateComponents()
components.calendar = Calendar.current
components.calendar?.locale = Locale(identifier: "ru_RU")
components.weekday = weekday
let date = Calendar.current.nextDate(after: Date(), matching: components, matchingPolicy: Calendar.MatchingPolicy.strict)
if date == nil {
return "E"
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEEE"
return dateFormatter.string(from: date!)
}
您的代码不必要地过于复杂。
你想要得到的只是weekday symbols
。尽可能最短的形式。
并且Calendar
为此专门属性:veryShortStandaloneWeekdaySymbols。
结果函数为:
func getWeekdayLabel(weekday: Int) -> String {
// TODO: Move this away, and reuse, it would be the same for every call ↓
var calendar = Calendar.autoupdatingCurrent
calendar.locale = Locale(identifier: "ru_RU") // Or any other locale, if you wan current, just drop this line
// TODO: ↑
return calendar.veryShortStandaloneWeekdaySymbols[weekday - 1] // because CalendarDateRangePickerViewController uses 1...7 weekday range, and here it's 0...6(or 0..<7)
}
如果您想要 weekdays
更详细地描述,请使用 See Also
部分中的其他 *weekdaySymbols
。
我正在使用此 pod https://github.com/miraan/CalendarDateRangePickerViewController 处理日历。我更改了月份名称,但无法更改星期几。
我尝试添加 components.calendar?.locale = Locale(identifier: "ru_RU") 和 dateFormatter.locale = Locale.autoupdatingCurrent 但没有任何改变。
func getWeekdayLabel(weekday: Int) -> String {
var components = DateComponents()
components.calendar = Calendar.current
components.calendar?.locale = Locale(identifier: "ru_RU")
components.weekday = weekday
let date = Calendar.current.nextDate(after: Date(), matching: components, matchingPolicy: Calendar.MatchingPolicy.strict)
if date == nil {
return "E"
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEEE"
return dateFormatter.string(from: date!)
}
您的代码不必要地过于复杂。
你想要得到的只是weekday symbols
。尽可能最短的形式。
并且Calendar
为此专门属性:veryShortStandaloneWeekdaySymbols。
结果函数为:
func getWeekdayLabel(weekday: Int) -> String {
// TODO: Move this away, and reuse, it would be the same for every call ↓
var calendar = Calendar.autoupdatingCurrent
calendar.locale = Locale(identifier: "ru_RU") // Or any other locale, if you wan current, just drop this line
// TODO: ↑
return calendar.veryShortStandaloneWeekdaySymbols[weekday - 1] // because CalendarDateRangePickerViewController uses 1...7 weekday range, and here it's 0...6(or 0..<7)
}
如果您想要 weekdays
更详细地描述,请使用 See Also
部分中的其他 *weekdaySymbols
。