如何快速从Date()每月获得一个月的所有4(或5)周?

时间:2019-01-25 08:46:34

标签: swift realm

我设法在特定月份使用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周(如果有)。每个单独的标题将仅显示该周的事件。我试图在日历中应用weekOfMonth,但是它不起作用。预先谢谢你。

2 个答案:

答案 0 :(得分:0)

以下是找出一周的第一天和最后一天的摘录。您可以通过在日期部分添加 .weekOfMonth 来实现。仔细阅读此official link并根据您的提取要求进行相应的应用。 现在,我添加了两个功能/或两个按钮操作,通过它们可以获取本月的上周和下周。

    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()
    }

答案 1 :(得分:0)

您可以通过两次操作从两个日期中获取星期范围

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

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

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