我需要帮助返回 UILocalNotification 中 2 个月之间的秒数

I need help returning the number of seconds between 2 months in UILocalNotification

我成功地计算了两个日期之间的秒数差异,但是如果是几个月,我对每个日期都使用了通用的 30 天,这在某些情况下是不正确的。 有没有更简单的方法。我需要在 UILocalNotification

中的时间间隔
@IBAction func createNotificationButton(sender: UIButton) {

    //Prepare Local notification
    let userCalendar = NSCalendar.currentCalendar()

    let currentDateAndTimeComponents = NSDateComponents()

    let currentDateAndTime = NSDate()
    println("CurrentDateAndTime: \(currentDateAndTime)")

    //Determining current time NSDates components
    let currentDateAndTimeComponentsVariables = userCalendar.dateWithEra(9223372036854775807,  year: currentDateAndTimeComponents.year
        , month: currentDateAndTimeComponents.month, day: currentDateAndTimeComponents.day, hour: currentDateAndTimeComponents.hour, minute: currentDateAndTimeComponents.minute, second: currentDateAndTimeComponents.second, nanosecond: 0)


    println("\(currentDateAndTimeComponents.era)")

    //Check if the value in () should be changed
    //   let currentDateAndTime = userCalendar.dateFromComponents(currentDateAndTimeComponents)!

    // Notification future time
    let futureNotificationDateComponents = NSDateComponents()
    futureNotificationDateComponents.era = 9223372036854775807
    futureNotificationDateComponents.year = yearTextField.text.toInt()!
    futureNotificationDateComponents.month = monthTextfield.text.toInt()!
    futureNotificationDateComponents.day = dayTextField.text.toInt()!
    futureNotificationDateComponents.hour = houtTextField.text.toInt()!
    futureNotificationDateComponents.minute = minuteTextField.text.toInt()!
    futureNotificationDateComponents.second = 0
    let futureNotificationDay = userCalendar.dateFromComponents(futureNotificationDateComponents)!

    let yearMonthDayHourMinuteSecond: NSCalendarUnit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitSecond




    let difference = NSCalendar.currentCalendar().components(yearMonthDayHourMinuteSecond, fromDate: currentDateAndTime, toDate: futureNotificationDay, options: nil)
    println("Difference: \(difference)")
    println("Era : \(difference.era)")
    println("Year: \(difference.year)")
    println("Month: \(difference.month)")
    println("Day: \(difference.day)")
    println("Hour: \(difference.hour)")
    println("Minute: \(difference.minute)")
    println("Second: \(difference.second)")

    var timeInterval = difference.second + difference.minute*60 + difference.hour*3600
    timeInterval = timeInterval + difference.day*3600*24 + difference.month*30*3600*24
    timeInterval = timeInterval + difference.year*365*24*3600

    println("Time interval: \(timeInterval)")

    let newTimeInterval = Double(timeInterval)
    let test = NSTimeZone.systemTimeZone()

    println("Time zone: \(test)")

    func LocalNotificationFunction() -> UILocalNotification {

        //Preparing a local notification to alert for the need to play the video message
        let LocalNotificationMessage:String = "Hello \n\n It's time to prepare to play the video message from PosterityMessages"
        localNotificationOfVideo.timeZone = NSTimeZone.systemTimeZone()

        println("In the function: \(localNotificationOfVideo.timeZone)")

        localNotificationOfVideo.fireDate = NSDate(timeIntervalSinceNow: newTimeInterval)
        localNotificationOfVideo.alertBody = LocalNotificationMessage
        localNotificationOfVideo.soundName = UILocalNotificationDefaultSoundName
        return localNotificationOfVideo

    }

    ScheduleLocalNotificationIfPossible()

}

假设需要两个月后的约会。

使用日期组件并递增月份组件:

let startDate = NSDate()
println("startDate: \(startDate)") // "startDate: 2015-07-12 12:48:41 +0000"

let calendar = NSCalendar.currentCalendar()
let units: NSCalendarUnit = .CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond
var components = calendar.components(units, fromDate: startDate)

components.month += 2
let futureDate = calendar.dateFromComponents(components)
println("futureDate: \(futureDate!)") // "futureDate: 2015-09-12 12:47:29 +0000"

以下两行的问题在于它只会给你一分钟的多余秒数:

let yearMonthDayHourMinuteSecond: NSCalendarUnit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitSecond

let difference = NSCalendar.currentCalendar().components(yearMonthDayHourMinuteSecond, fromDate: currentDateAndTime, toDate: futureNotificationDay, options: nil)

如果您想检索两个日期之间的实际秒数,您必须指定您只想检索秒数:

let yearMonthDayHourMinuteSecond: NSCalendarUnit = NSCalendarUnit.CalendarUnitSecond

let difference = NSCalendar.currentCalendar().components(yearMonthDayHourMinuteSecond, fromDate: currentDateAndTime, toDate: futureNotificationDay, options: nil)

一个月也一样。如果您将 Month 和 Second 合并到一个语句中,您将只会获得每个月的多余秒数。很难解释/理解。如果您计算现在和一小时后的差值,您将得到 3600 秒。如果您计算现在和(下个月的一小时内)之间的差异,您仍然只会得到 3600 秒。仅当您省略 Month 部分时,您才会检索到实际秒数。

希望对您有所帮助。您还需要进一步说明吗?月份代码为

let yearMonthDayHourMinuteSecond: NSCalendarUnit = NSCalendarUnit.CalendarUnitMonth 

let difference = NSCalendar.currentCalendar().components(yearMonthDayHourMinuteSecond, fromDate: currentDateAndTime, toDate: futureNotificationDay, options: nil)