如何在 swift 中从 Date() 获取一个月内所有 4(或 5)个单独的星期



我设法在特定月份使用类型为Date()的"nextDate"属性获取数据查询。换句话说,查询时,具有该特定月份内日期的所有数据都将出现在我的表视图中。使用下面的代码并进行调整,我还设法提取了上个月和下个月的数据。

func loadMonthEvents() {
    let date = Date()
    let calendar = Calendar.current
    var beginningOfMonth: Date?
    var endOfMonth: Date?
    beginningOfMonth = calendar.dateInterval(of: .month, for: date)?.start
        endOfMonth = calendar.dateInterval(of: .month, for: date)?.end
    monthEvents = realm.objects(Events.self).filter("nextDate BETWEEN %@", [beginningOfMonth, endOfMonth]).sorted(byKeyPath: "nextDate", ascending: true)
}

现在,我希望能够根据每月的星期分隔这些数据。在我的 tableView 中,将是 5 个单独的标题,分别代表第 1 周、第 2 周、第 3 周、第 4 周和第 5 周(如果有)。每个单独的标题将仅显示该周的事件。我试图在日历中应用月周,但它不起作用。提前谢谢你。

你可以通过

let w1 = calendar.dateComponents([.weekOfYear], from: beginningOfMonth!)
print(w1.weekOfYear)
let w2 = calendar.dateComponents([.weekOfYear], from: endOfMonth!)
print(w2.weekOfYear)

然后,您可以对每个事件的日期执行相同的操作,将事件分组到特定的一周

这是找出一周的第一天和最后一天的片段。您可以通过将 .weekOfMonth 添加到日期组件来执行此操作。浏览此官方链接并根据您的获取要求进行相应申请。现在我添加了两个功能/或两个按钮操作,您可以通过它们获得本月的前一周和下周。

    var currentDate = Date()
    func weekCalculation()
    {
        let calendar = NSCalendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
//        dateFormatter.dateStyle = .medium
        var componentsNow = calendar.dateComponents([.year, .month, .weekOfMonth, .weekday], from: currentDate)
        componentsNow.setValue(1, for: .weekday)
        firstDayOfWeek = calendar.date(from: componentsNow)!
        print(firstDayOfWeek)
        componentsNow.setValue(7, for: .weekday)
        lastDayOfWeek = calendar.date(from: componentsNow)!
        print(lastDayOfWeek)
        let addDaysCount = 0
        var comps = DateComponents()
        comps.setValue(addDaysCount, for: .weekday)
        var comps1 = DateComponents()
        comps1.setValue(-6, for: .day)
        let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
        let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
//        print(newDate1!,newDate2!)
        let firstDay = dateFormatter.string(from: newDate1!)
        let lastDay = dateFormatter.string(from: newDate2!)
//        ShowBanner(title: "", subtitle: firstDay)
        let dF = DateFormatter()
        dF.dateFormat = "d MMMM yyyy"
        let fDayToShow = dF.string(from: newDate1!)
        let lDayToShow = dF.string(from: newDate2!)
        let dateString = String(format: "%@ - %@",fDayToShow,lDayToShow)
        print(firstDay,lastDay)
    }
    @IBAction func nextWeekBtnPressed(sender: UIButton)
    {
        let calendar = NSCalendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
//        dateFormatter.dateStyle = .medium
        let dF = DateFormatter()
        dF.dateFormat = "d MMMM yyyy"
        let addDaysCount = 7
        var comps = DateComponents()
        comps.setValue(addDaysCount, for: .weekday)
        var comps1 = DateComponents()
        comps1.setValue(3, for: .day)
        let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
        let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
        let firstDay = dateFormatter.string(from: newDate1!)
        let lastDay = dateFormatter.string(from: newDate2!)
        let fDayToShow = dF.string(from: newDate1!)
        let lDayToShow = dF.string(from: newDate2!)
        //print(firstDay,lastDay)
        let dateString = String(format: "%@ - %@",fDayToShow,lDayToShow)
        //print(dateString)
        weekCalculation()
    }
    @IBAction func previousWeekBtnPressed(sender: UIButton)
    {
            let calendar = NSCalendar.current
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
//            dateFormatter.dateStyle = .medium
            let addDaysCount = -7
            var comps = DateComponents()
            comps.setValue(addDaysCount, for: .weekday)
        var comps1 = DateComponents()
        comps1.setValue(-10, for: .day)
        let newDate1 = calendar.date(byAdding: comps1, to: lastDayOfWeek)
            let newDate2 = calendar.date(byAdding: comps, to: lastDayOfWeek)
            let firstDay = dateFormatter.string(from: newDate1!)
            let lastDay = dateFormatter.string(from: newDate2!)
            let dF = DateFormatter()
            dF.dateFormat = "d MMMM yyyy"
            let fDayToShow = dF.string(from: newDate1!)
            let lDayToShow = dF.string(from: newDate2!)
            let dateString = String(format: "%@ - %@",fDayToShow,lDayToShow)
        weekCalculation()
    }

最新更新