0

I need to schedule a notification. If the date made from the current date date components with the dateFromData time components is early than the current day, I would like to change it to the next day. Here is what I have so far. The date comparison dose not work no matter how I set it. It either always changes it or never changes it.

var dateComps = calendar.components(NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate())

dateComps.hour = calendar.component(NSCalendarUnit.HourCalendarUnit, fromDate: dateFromData)
dateComps.minute = calendar.component(NSCalendarUnit.MinuteCalendarUnit, fromDate: dateFromData)


    if fireDate.compare(NSDate()) == NSComparisonResult.OrderedDescending {
        //change day to next day

        dateComps.day += 1
        println("Change day")
    }else{
        println("Do not change day")
    }




    let notifactionOfAmountOfWork = UILocalNotification()
        notifactionOfAmountOfWork.category = "normalNotifactionCatagory"

    notifactionOfAmountOfWork.fireDate = calendar.dateFromComponents(dateComps)

1 Answers1

0

How your firstDate defined? The following program works, I just tried it. You could change comps1.day and comps2.day to let it hit both conditions.

import UIKit

let calendar1 = NSCalendar.currentCalendar()
let comps1 = NSDateComponents()
comps1.day = 7
let date1 = calendar1.dateByAddingComponents(comps1, toDate: NSDate(), options: NSCalendarOptions.allZeros)

let calendar2 = NSCalendar.currentCalendar()
let comps2 = NSDateComponents()
comps2.day = 0
let date2 = calendar2.dateByAddingComponents(comps2, toDate: NSDate(), options: NSCalendarOptions.allZeros)


if date1?.compare(date2!) == NSComparisonResult.OrderedDescending
{
    println("date1 is after date 2")
}else{
    println("date1 is before date 2")
}
Zhenyi Luo
  • 61
  • 5