gpt4 book ai didi

swift - 如何快速创建当前星期日期的数组

转载 作者:行者123 更新时间:2023-12-02 02:49:04 27 4
gpt4 key购买 nike

我正在开展一个项目,我需要本周的日期。我目前正在使用此函数来获取当前日期。

func getDay() -> String {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let result = formatter.string(from: date)
return result
}

它可以很好地为我提供一个约会,但我不知道如何获得本周的所有 7 个日期。如果有人可以提供帮助,我们将不胜感激。

最佳答案

您可以获得今天的yearForWeekOfYear 和weekOfYear 日历组件并从中构造一个新日期。它将为您提供一周的第一天。如果您希望一周从星期一开始,您需要使用 iso8601 日历,如果您希望一周从星期日开始,您可以使用公历。为了避免一些极端情况,您应该使用中午时间进行所有日历计算。并非所有日期都从午夜开始。

let dateComponents = Calendar(identifier: .gregorian).dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date())
let startOfWeek = Calendar(identifier: .gregorian).date(from: dateComponents)!
let startOfWeekNoon = Calendar(identifier: .gregorian).date(bySettingHour: 12, minute: 0, second: 0, of: startOfWeek)!
let weekDays = (0...6).map { Calendar(identifier: .gregorian).date(byAdding: .day, value: $0, to: startOfWeekNoon)! }
weekDays // ["Jun 7, 2020 at 12:00 PM", "Jun 8, 2020 at 12:00 PM", "Jun 9, 2020 at 12:00 PM", "Jun 10, 2020 at 12:00 PM", "Jun 11, 2020 at 12:00 PM", "Jun 12, 2020 at 12:00 PM", "Jun 13, 2020 at 12:00 PM"]

在此基础上,您可以扩展 Date 并创建一些助手:

extension Calendar {
static let iso8601 = Calendar(identifier: .iso8601)
static let gregorian = Calendar(identifier: .gregorian)
}

extension Date {
func byAdding(component: Calendar.Component, value: Int, wrappingComponents: Bool = false, using calendar: Calendar = .current) -> Date? {
calendar.date(byAdding: component, value: value, to: self, wrappingComponents: wrappingComponents)
}
func dateComponents(_ components: Set<Calendar.Component>, using calendar: Calendar = .current) -> DateComponents {
calendar.dateComponents(components, from: self)
}
func startOfWeek(using calendar: Calendar = .current) -> Date {
calendar.date(from: dateComponents([.yearForWeekOfYear, .weekOfYear], using: calendar))!
}
var noon: Date {
Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
func daysOfWeek(using calendar: Calendar = .current) -> [Date] {
let startOfWeek = self.startOfWeek(using: calendar).noon
return (0...6).map { startOfWeek.byAdding(component: .day, value: $0, using: calendar)! }
}
}

用法:

// this will give you all days of the current week starting monday
Date().daysOfWeek(using: .iso8601) // ["Jun 8, 2020 at 12:00 PM", "Jun 9, 2020 at 12:00 PM", "Jun 10, 2020 at 12:00 PM", "Jun 11, 2020 at 12:00 PM", "Jun 12, 2020 at 12:00 PM", "Jun 13, 2020 at 12:00 PM", "Jun 14, 2020 at 12:00 PM"]
// this will give you all days of the current week starting sunday
Date().daysOfWeek(using: .gregorian) // ["Jun 7, 2020 at 12:00 PM", "Jun 8, 2020 at 12:00 PM", "Jun 9, 2020 at 12:00 PM", "Jun 10, 2020 at 12:00 PM", "Jun 11, 2020 at 12:00 PM", "Jun 12, 2020 at 12:00 PM", "Jun 13, 2020 at 12:00 PM"]

要将日期转换为固定日期格式,您可以创建一个扩展格式化程序和日期的自定义日期格式化程序:

extension Formatter {
static let ddMMyyyy: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "dd.MM.yyyy"
return dateFormatter
}()
}

extension Date {
var ddMMyyyy: String { Formatter.ddMMyyyy.string(from: self) }
}

现在您可以使用 KeyPath 简单地映射日期:

Date().daysOfWeek(using: .gregorian).map(\.ddMMyyyy) // ["07.06.2020", "08.06.2020", "09.06.2020", "10.06.2020", "11.06.2020", "12.06.2020", "13.06.2020"]

关于swift - 如何快速创建当前星期日期的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62355030/

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