NSDate - 返回最近的小时

时间:2016-01-03 23:30:34

标签: ios swift nsdate

我想创建一个项目:

  1. 我创建了一个要执行的任务。
  2. 我为此任务创建了一个停止日期
  3. 我使用日期选择器创建三个提醒
  4. 我已经完成了这件事。但是现在,我希望标签能够在最近的提醒时间内返回给我(但我希望标签在停止日期之前执行此操作)

    我已经完成的是一个游乐场项目:

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    let currentDate = dateFormatter.dateFromString("2016-01-03 16:30")
    
    var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
    var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
    var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
    var datesArray = [firstDate, secondDate, thirdDate]
    
    
    let firstInterval = firstDate?.timeIntervalSinceDate(currentDate!)
    let secondInterval = secondDate?.timeIntervalSinceDate(currentDate!)
    let thirdInterval = thirdDate?.timeIntervalSinceDate(currentDate!)
    let intervalArray = [firstInterval, secondInterval, thirdInterval]
    
    
    var aboveZeroIntervals = [NSTimeInterval]()
    for interval in intervalArray {
    if interval > 0 {
        aboveZeroIntervals.append(interval!)
    }
    }
    //print(aboveZeroIntervals)
    
    for date in datesArray {
    if date?.timeIntervalSinceDate(currentDate!) == aboveZeroIntervals.minElement() {
        print(dateFormatter.stringFromDate(date!))
    }
    }
    

    如何从第一个/第二个/第三个日期获得小时数,并显示距离当前日期最近的三个日期的小时数?并且在currentDate超过stopDoingDate后完美地显示它...

    我想出了类似的东西(下面的代码)。谁能看到它并告诉我是否有某种方法可以简化这个?

    extension NSDate
    {
    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isGreater = false
    
        //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
            isGreater = true
        }
    
        //Return Result
        return isGreater
    }
    }
    
    let calendar = NSCalendar.currentCalendar()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    let currentDate = dateFormatter.dateFromString("2016-01-04 16:30")
    let stopDoingDate = dateFormatter.dateFromString("2016-01-06 15:00")
    
    
    if stopDoingDate!.isGreaterThanDate(currentDate!) {
    let currentDateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute], fromDate: currentDate!)
    currentDateComponents.hour
    currentDateComponents.minute
    let dateTimeFormatter = NSDateFormatter()
    dateTimeFormatter.dateFormat = "HH:mm"
    let currentOnlyTime = dateTimeFormatter.dateFromString("\(currentDateComponents.hour):\(currentDateComponents.minute)")
    
    var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
    var firstDateComponents = calendar.components([.Hour, .Minute], fromDate: firstDate!)
    var firstDateTime = dateTimeFormatter.dateFromString("\(firstDateComponents.hour):\(firstDateComponents.minute)")
    
    var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
    var secondDateComponents = calendar.components([.Hour, .Minute], fromDate: secondDate!)
    var secondDateTime = dateTimeFormatter.dateFromString("\(secondDateComponents.hour):\(secondDateComponents.minute)")
    
    var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
    var thirdDateComponents = calendar.components([.Hour, .Minute], fromDate: thirdDate!)
    var thirdDateTime = dateTimeFormatter.dateFromString("\(thirdDateComponents.hour):\(thirdDateComponents.minute)")
    
    var datesArray = [firstDateTime, secondDateTime, thirdDateTime]
    
    
    let firstInterval = firstDateTime?.timeIntervalSinceDate(currentOnlyTime!)
    let secondInterval = secondDateTime?.timeIntervalSinceDate(currentOnlyTime!)
    let thirdInterval = thirdDateTime?.timeIntervalSinceDate(currentOnlyTime!)
    let intervalArray = [firstInterval, secondInterval, thirdInterval]
    
    
    var aboveZeroIntervals = [NSTimeInterval]()
    for interval in intervalArray {
        if interval > 0 {
            aboveZeroIntervals.append(interval!)
        }
    }
    //print(aboveZeroIntervals)
    for date in datesArray {
        if date?.timeIntervalSinceDate(currentOnlyTime!) == aboveZeroIntervals.minElement() {
            let dateTime = calendar.components([.Hour, .Minute], fromDate: date!)
    
            print(dateFormatter.dateFromString("\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) \(dateTime.hour):\(dateTime.minute)")!)
            print("Next todo hour is: \(dateTime.hour): \(dateTime.minute)")
    
        }
    }
    } else {
    print("Todo DONE")
    }
    

1 个答案:

答案 0 :(得分:0)

我想出了这个: 唯一的问题是生成从currentDate到stopDoingDate的日期数组,但我会单独处理这个问题(也可能在这里发布一个解决方案)

我使用此论坛帖子中的一个来生成NSDate的扩展名:

extension NSDate
{
    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isGreater = false

        //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
            isGreater = true
        }

        //Return Result
        return isGreater
    }
}

 //let calendar = NSCalendar.currentCalendar()
 let dateFormatter = NSDateFormatter()
 dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
 let currentDate = dateFormatter.dateFromString("2016-01-04 14:00")!
 let stopDoingDate = dateFormatter.dateFromString("2016-01-06 16:00")!
 let date1 = dateFormatter.dateFromString("2016-01-04 09:00")
 let date2 = dateFormatter.dateFromString("2016-01-04 15:00")
 let date3 = dateFormatter.dateFromString("2016-01-05 09:00")
 let date4 = dateFormatter.dateFromString("2016-01-05 15:00")
 let date5 = dateFormatter.dateFromString("2016-01-06 09:00")
 let date6 = dateFormatter.dateFromString("2016-01-06 15:00")
 var dates: [NSDate] = [date1!, date2!, date3!, date6!, date5!, date4!]

 if stopDoingDate.isGreaterThanDate(currentDate) {
     var datesAfterCurrentDate = [NSDate]()
     for date in dates {
         if date.isGreaterThanDate(currentDate) {
             datesAfterCurrentDate.append(date)
        }
    }
     let sortedDatesAfterCurrentDate = datesAfterCurrentDate.sort({ $0.timeIntervalSince1970 < $1.timeIntervalSince1970 })
     let dateToDisplay = sortedDatesAfterCurrentDate.first!
     let timeFormatter = NSDateFormatter()
     timeFormatter.timeStyle = .ShortStyle
     print(timeFormatter.stringFromDate(dateToDisplay)) } else {
     print("We are done here")
}

这个论坛真棒!感谢Leo Dabus的简单想法!