gpt4 book ai didi

cocoa - 正确使用Apple Foundation Calendar

转载 作者:行者123 更新时间:2023-12-03 17:01:03 25 4
gpt4 key购买 nike

我对 Apple Foundations 框架的 Calendar 的正确使用有点困惑。

let calendar = Calendar(identifier: .iso8601)
let dayComponent = DateComponents(year: 2019, weekday: 1, weekOfYear: 6)
let date = calendar.date(from: dayComponent)

我需要获取一年中给定周的第一天。使用上面的代码时,根据工作日给出以下日期:

//weekday:
//0 -> 08 FEB
//1 -> 09 FEB
//2 -> 03 FEB
//3 -> 04 FEB
//4 -> 05 FEB

为什么当前周 (6) 的工作日从 0 开始,而增加后切换到第 5 周?

感谢您的帮助。

最佳答案

一些观察结果:

  1. 迭代工作日时,您希望从 1 到 7,因为“日、周、工作日、月和年数字通常是从 1 开始的...”Date and Time Programming Guide: Date Components and Calendar Units 。您可以使用range(of:in:for:) , maximumRange(of:)等,找出可能值的范围。

  2. 从 1 到 7 的工作日值并不表示“一周的第一天”、“一周的第二天”等。它们指的是一周中的特定日期,例如对于 .iso8601,“Sun”为 1,“Mon”为 2,依此类推。

  3. 确保在使用 weekOfYear 时使用 yearForWeekOfYear:

    let calendar = Calendar(identifier: .iso8601)
    let firstOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!
  4. 您的代码在工作日进行迭代。考虑以下代码,该代码枚举 2019 年第六周(即从 2 月 4 日星期一开始到 2 月 10 日星期日结束的一周)的所有日期:

    let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: firstOfWeek)!
    let daysOfTheWeek = Dictionary(uniqueKeysWithValues: zip(weekdays, calendar.weekdaySymbols))

    for weekday in weekdays {
    let date = DateComponents(calendar: calendar, weekday: weekday, weekOfYear: 6, yearForWeekOfYear: 2019).date!
    print("The", daysOfTheWeek[weekday]!, "in the 6th week of 2019 is", formatter.string(from: date))
    }

    结果是:

    The Sun in the 6th week of 2019 is Sunday, February 10, 2019
    The Mon in the 6th week of 2019 is Monday, February 4, 2019
    The Tue in the 6th week of 2019 is Tuesday, February 5, 2019
    The Wed in the 6th week of 2019 is Wednesday, February 6, 2019
    The Thu in the 6th week of 2019 is Thursday, February 7, 2019
    The Fri in the 6th week of 2019 is Friday, February 8, 2019
    The Sat in the 6th week of 2019 is Saturday, February 9, 2019

    这实际上就是您的代码所做的事情,也是您没有看到预期结果的原因。

  5. 如果您想按顺序遍历一周中的 7 天,只需获取一周的开始,然后从那里偏移即可:

    let calendar = Calendar(identifier: .iso8601)
    let startOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!

    for offset in 0 ..< 7 {
    let date = calendar.date(byAdding: .day, value: offset, to: startOfWeek)!
    print(offset, "->", formatter.string(from: date))
    }

    结果是:

    0 -> Monday, February 4, 2019
    1 -> Tuesday, February 5, 2019
    2 -> Wednesday, February 6, 2019
    3 -> Thursday, February 7, 2019
    4 -> Friday, February 8, 2019
    5 -> Saturday, February 9, 2019
    6 -> Sunday, February 10, 2019

  6. 您问:

    I need to get the first day of a given week of year.

    此时可能无需多言,只需省略工作日即可:

    let startOfWeek = DateComponents(calendar: calendar, weekOfYear: 6, yearForWeekOfYear: 2019).date!

另请参阅Date and Time Programming Guide: Week-Based Calendars .

关于cocoa - 正确使用Apple Foundation Calendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54608089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com