获得两个日期的差异 - 斯威夫特

时间:2016-09-21 22:20:36

标签: ios swift nsdate

我无法使用日期差异功能。它说NSDate不能隐含地转换为'日期'但我没有看到立即解决这个问题;使用as Date无效。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var dateLabelOutlet: UILabel!

    let currentDate = NSDate()
    let dateFormatter = DateFormatter()

    let userCalendar = NSCalendar.current

    let requestedComponent: NSCalendar.Unit = [
        NSCalendar.Unit.month,
        NSCalendar.Unit.day,
        NSCalendar.Unit.hour,
        NSCalendar.Unit.minute,
        NSCalendar.Unit.second,

    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func printTime(){
        dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss a"
        let startTime = NSDate()
        let endTime = dateFormatter.date(from: "25/12/16 00:00:00")

        let timeDifference = userCalendar.components(requestedComponent, from: startTime, to: endTime!, options: [])

        dateLabelOutlet.text = "\(timeDifference.month) Months \(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

2 个答案:

答案 0 :(得分:12)

将您的所有NSDate更改为Date,然后将requestedComponent替换为:

let requestedComponent: Set<Calendar.Component> = [ .month, .day, .hour, .minute, .second]

你的不同之处在于:

let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)

仅供参考:您的dateFormatter无效“25/12/16 00:00:00” 这是你的整个班级的正确形式:

class ViewController: UIViewController {

    @IBOutlet weak var dateLabelOutlet: UILabel!

    let currentDate = Date()
    let dateFormatter = DateFormatter()

    let userCalendar = Calendar.current

    let requestedComponent: Set<Calendar.Component> = [.month,.day,.hour,.minute,.second]

    override func viewDidLoad() {
        super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.
    }

    func printTime() {
        dateFormatter.dateFormat = "dd/MM/yy hh:mm:ss"
        let startTime = Date()
        let endTime = dateFormatter.date(from: "25/12/16 00:00:00")
        let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)

        dateLabelOutlet.text = "\(timeDifference.month) Months \(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds"
    }

    override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
     }
}

答案 1 :(得分:0)

我认为这会有所帮助。它将打印从两个日期之间的所有月份

   let dateFormtter = DateFormatter()
    dateFormtter.dateFormat = "yyyy-MM-dd"

    let startDate = dateFormtter.date(from: fromTF.text!)
    let endDate = dateFormtter.date(from: toTF.text!)

 func getMonthAndYearBetween(from start: String, to end: String) - > 
[String] {

    let format = DateFormatter()
    format.dateFormat = "yyyy-MM-dd"

    guard let startDate = format.date(from: start),
        let endDate = format.date(from: end) else {
            return []
    }

    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(Set([.month,.day]), 
   from: startDate, to: endDate)

    var allDates: [String] = []
    let dateRangeFormatter = DateFormatter()
    dateRangeFormatter.dateFormat = "yyyy-MM-dd"

    for i in 0 ... components.month! {
        guard let date = calendar.date(byAdding: .month , value: i, to: startDate) else {
            continue
        }

        let formattedDate = dateRangeFormatter.string(from: date)
        allDates += [formattedDate]

        print(allDates)
    }
    return allDates
}

像上面这样调用上面的方法

     print(getMonthAndYearBetween(from: fromTF.text!, to: 
                    toTF.text!))