工作日组件问题

Weekday component issue

我正在尝试使用日历中的工作日。我应该在星期一收到,但不知何故我在星期二收到。有什么想法吗?

let date = Date(timeIntervalSince1970: 1519654139)
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "en_US_POSIX")
calendar.timeZone = TimeZone(secondsFromGMT: 3600)!

let weekDayComponent = calendar.component(Calendar.Component.weekday, from: date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE EEEE"
print("Date formatter says it's " + dateFormatter.string(from: date))
print("Weekday component is \(weekDayComponent)")

控制台输出:

Date formatter says it's Mon Monday
Weekday component is 2

[编辑]:为什么我收到 2 个,而不是 1 个?是的,calendar.firstWeekday 的值为 1。

公历中的星期从星期日开始,因此这是正确的行为,2 是星期一。 Documentation 明确指出:

Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.