如何制作当前周日期的数组 Swift [英] How to make an array of the current week dates Swift

查看:38
本文介绍了如何制作当前周日期的数组 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个需要本周日期的项目.我目前正在使用此函数来获取当前日期.

I am working on a project where I need the dates of the current week. I am currently using this function to get the date of the present day.

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

让我获得一个日期效果很好,但我不知道如何获得本周的所有 7 个日期.如果有人可以提供帮助,将不胜感激.

It works fine for getting me the one date, but I don't know how to get all 7 dates of the current week. If anyone can help it would be greatly appreciated.

推荐答案

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

You can get today's yearForWeekOfYear and weekOfYear calendar components and construct a new date from it. It will give you the first day of the week. If you want your week to start from Monday you need to use iso8601 calendar, if you would like to have your week to start on Sunday you can use Gregorian calendar. To avoid some extreme cases you should do all your calendrical calculations using noon time. Not all dates starts at midnight.

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 并创建一些帮助程序:


Expanding on that you can extend Date and create some helpers:

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

<小时>

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


To convert your date to a fixed date format you can create a custom date formatter extending Formatter and Date:

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 映射您的日期:


Now you can simply map your dates using the 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆