虽然日期不在2个日期之内,但请将日期增加2个日期。迅速

时间:2016-03-25 10:35:17

标签: swift nsdate nscalendar

我有以下代码,其中包含一个由用户选择的选定日期的函数。这传入了一个currentStart和endDate,它是从CoreData中检索出来的,我想根据传递给下面函数的前一个startDate和endDate创建一个newStartDate和newEndDate:

sudo tcpdump -i wlan0 -e ether host 90:B6:86:15:A9:DB -vvv -w capture.pcap

它似乎最初起作用。例如,当currentStartDate是2016年3月24日时,这会成功更改为2016年3月25日,但这永远不会更改为2016年3月26日。

输出如下所示:

func calcStartAndEndDateBasedOnCurrentExsisting(selectedSpendDate: NSDate, currentStartDate: NSDate,
    currentEndDate: NSDate, durationOccurance: Double) {
    //initialising the newStartDate and newEndDate
    var newStartDate : NSDate = currentStartDate
    var newEndDate : NSDate = currentEndDate

    var occurance : Int
    //if the selectedSpendDate is before the currentStartDate, make the occurance a negative so it goes back in time. durationOccurance is how many days to increase/decrease by
    if selectedSpendDate.earlierDate(currentStartDate).isEqualToDate(selectedSpendDate)  {
        occurance = -Int(durationOccurance)

    }
    else {
        occurrence = Int(durationOccurance)
    }


        print("old end date = \(currentEndDate)")
        components.day = occurance

        while newStartDate.compare(selectedSpendDate) == .OrderedDescending || newEndDate.compare(selectedSpendDate) == .OrderedAscending {
            print("\(selectedSpendDate) is not within \(newStartDate) & \(newEndDate)")

            let test = newStartDate
            print("old start date = \(test)")
            let newDate = calendar.dateByAddingComponents(components, toDate: test, options: [])!

            newStartDate = newDate
            print("new StartDate = \(newStartDate)")

            //working out end date from the computed start date
            let calculatedEndDay = rangeOfPeriod(.Day, date: newStartDate).1
            components.day = Int(durationOccurance) - 1

            newEndDate = calendar.dateByAddingComponents(components, toDate: calculatedEndDay, options: [])!
            print("Start: \(newStartDate) End: \(newEndDate)")
        }

}

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

components.day = occurance在while循环中

时,它按预期工作