Home > Back-end >  Swift: Why DateComponentsFormatter.localizedString(from:unitsStyle:) does not work for weekday?
Swift: Why DateComponentsFormatter.localizedString(from:unitsStyle:) does not work for weekday?

Time:02-06

Why DateComponentsFormatter.localizedString(from:unitsStyle:) does not work for weekday?

let twoDays = DateComponents(day: 2)
let threeWeeks = DateComponents(weekday: 3)

DateComponentsFormatter.localizedString(from: twoDays, unitsStyle: .full) ?? "found nil" // "2 days"
DateComponentsFormatter.localizedString(from: threeWeeks, unitsStyle: .full) ?? "found nil" // returns "found nil"

CodePudding user response:

The DateComponents has a property named allowedUnits, weekday is not a legal unit.

The allowed units are:

year month weekOfMonth day hour minute second

see this ref.

  •  Tags:  
  • Related