Swift days between two NSDates

后端 未结 27 1202
清歌不尽
清歌不尽 2020-11-27 13:30

I\'m wondering if there is some new and awesome possibility to get the amount of days between two NSDates in Swift / the \"new\" Cocoa?

E.g. like in Ruby I would do:

相关标签:
27条回答
  • 2020-11-27 13:32

    You have to consider the time difference as well. For example if you compare the dates 2015-01-01 10:00 and 2015-01-02 09:00, days between those dates will return as 0 (zero) since the difference between those dates is less than 24 hours (it's 23 hours).

    If your purpose is to get the exact day number between two dates, you can work around this issue like this:

    // Assuming that firstDate and secondDate are defined
    // ...
    
    let calendar = NSCalendar.currentCalendar()
    
    // Replace the hour (time) of both dates with 00:00
    let date1 = calendar.startOfDayForDate(firstDate)
    let date2 = calendar.startOfDayForDate(secondDate)
    
    let flags = NSCalendarUnit.Day
    let components = calendar.components(flags, fromDate: date1, toDate: date2, options: [])
    
    components.day  // This will return the number of day(s) between dates
    

    Swift 3 and Swift 4 Version

    let calendar = Calendar.current
    
    // Replace the hour (time) of both dates with 00:00
    let date1 = calendar.startOfDay(for: firstDate)
    let date2 = calendar.startOfDay(for: secondDate)
    
    let components = calendar.dateComponents([.day], from: date1, to: date2)
    
    0 讨论(0)
  • 2020-11-27 13:32

    Update for Swift 3 iOS 10 Beta 4

    func daysBetweenDates(startDate: Date, endDate: Date) -> Int {
        let calendar = Calendar.current
        let components = calendar.dateComponents([Calendar.Component.day], from: startDate, to: endDate)
        return components.day!
    }
    
    0 讨论(0)
  • 2020-11-27 13:32

    Swift 5.2.4 solution:

    import UIKit
    
    let calendar = Calendar.current
    
    let start = "2010-09-01"
    let end = "2010-09-05"
    
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    
    let firstDate = dateFormatter.date(from: start)!
    let secondDate = dateFormatter.date(from: end)!
    
    // Replace the hour (time) of both dates with 00:00
    let date1 = calendar.startOfDay(for: firstDate)
    let date2 = calendar.startOfDay(for: secondDate)
    
    let components = calendar.dateComponents([Calendar.Component.day], from: date1, to: date2)
    
    components.day  // This will return the number of day(s) between dates
    
    0 讨论(0)
  • 2020-11-27 13:33

    Here is my answer for Swift 2:

    func daysBetweenDates(startDate: NSDate, endDate: NSDate) -> Int
    {
        let calendar = NSCalendar.currentCalendar()
    
        let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
    
        return components.day
    }
    
    0 讨论(0)
  • 2020-11-27 13:33

    Here is very nice, Date extension to get difference between dates in years, months, days, hours, minutes, seconds

    extension Date {
    
        func years(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.year], from: sinceDate, to: self).year
        }
    
        func months(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.month], from: sinceDate, to: self).month
        }
    
        func days(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.day], from: sinceDate, to: self).day
        }
    
        func hours(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.hour], from: sinceDate, to: self).hour
        }
    
        func minutes(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.minute], from: sinceDate, to: self).minute
        }
    
        func seconds(sinceDate: Date) -> Int? {
            return Calendar.current.dateComponents([.second], from: sinceDate, to: self).second
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 13:33
    extension Date {
        func daysFromToday() -> Int {
            return Calendar.current.dateComponents([.day], from: self, to: Date()).day!
        }
    }
    

    Then use it like

        func dayCount(dateString: String) -> String{
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMM dd,yyyy hh:mm a"
            let fetchedDate = dateFormatter.date(from: dateString)
    
    
            let day = fetchedDate?.daysFromToday()
            if day! > -1{
                return "\(day!) days passed."
            }else{
            return "\(day! * -1) days left."
            }
        }
    
    0 讨论(0)
提交回复
热议问题