【问题标题】:Weekday component issue工作日组件问题
【发布时间】:2018-02-26 22:38:46
【问题描述】:

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

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)")

控制台输出:

日期格式化程序显示是星期一
工作日分量是 2

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

【问题讨论】:

标签: ios iphone swift nsdateformatter


【解决方案1】:

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

工作日单位是从 1 到 n 的数字,其中 n 是一周中的天数。例如,在公历中,n 为 7,星期日用 1 表示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多