2016-11-18 68 views
7

如何比較Swift 3中的兩個日期? 我發現了許多其他Swift版本的解決方案,但它們不適合我。我如何比較Swift 3中的兩個日期(NSDate)並獲得它們之間的日期?

let clickedDay:String = currentcell.DayLabel.text! 
    let currentDate = NSDate() 
    let currentDay = "" //want the current day 
    let dateFormatter = DateFormatter()   
    var dateAsString = ""   
    if Int(clickedDay)! < 10 {    
     dateAsString = "0" + clickedDay + "-11-2016 GMT" 
    } 
    else {    
     dateAsString = clickedDay + "-11-2016 GMT" 
    } 

    dateFormatter.dateFormat = "dd-MM-yyyy zzz" 
    let clickedDate = dateFormatter.date(from: dateAsString)   
    if currentDate as Date >= clickedDate! as Date { 

     return true 
    } 
    else { 
     //Want to get the days between the currentDate and clickedDate 
     let daysLeft = "" 
    } 

我比較兩個日期,但我想要在這兩個日期之間的日子。 有人可以幫我嗎? 感謝和問候

補充: 如果我有日期2016年11月22日和的currentdate(現爲2016年11月18日),我想,之間的天數(在這種情況下4)

回答

11

只需做延伸和以天爲單位獲得的差別。

extension Date { 
    func daysBetweenDate(toDate: Date) -> Int { 
     let components = Calendar.current.dateComponents([.day], from: self, to: toDate) 
     return components.day ?? 0 
    } 
} 

現在使用這個擴展這樣

let numOfDays = fromDate.daysBetweenDate(toDate: toDate) 

您可以使用DateFormatter到字符串轉換爲這樣的日期。

let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "dd-MM-yyyy" 
dateFormatter.timeZone = TimeZone(abbreviation: "GMT") 
let date = dateFormatter.date(from: "18-11-2016") 
+0

我在哪裏宣佈延期? – MMbach

+0

獲取此錯誤:使用未解析的標識符'fromDate' – MMbach

+0

這有效,但如果我有18.11.2016(fromdate)和22.11.2016(todate),它說3天 – MMbach

1

嘗試:

let days=Calendar.current.dateComponents([.day], from: date_1, to: date_2) 
let nb_days=days.day 
+0

我想要這些日子,多長時間到下一個日期 – MMbach

+0

我不確定我是否理解你。我的回答給你你的日期和Date()之間的天數(所以今天)。但是,如果您想與另一個日期進行比較,只需使用next_date更改Date()。 –

+0

是的,這就是我想要的。但在幾天內我沒有號碼,我怎麼得到這個號碼? – MMbach

2

使用此函數來獲取兩個日期之間的所有中間日期,只是檢查下日期(截止日期)比起來日期(第一天),則順序爲+1更大,否則爲了-1: -

func selectDates(from upDate : Date, to downDate : Date, order : Int){ 
     var selectDate = Calendar.current.date(byAdding: .day, value: order, to: upDate)! 
     while compareDate(dateInitial: selectDate, dateFinal: downDate) != true{ 
      print(" intermediate date is \(selectDate)") 
      selectDate = Calendar.current.date(byAdding: .day, value: order, to: selectDate)! 
     } 
     print("*** intermediate dates are selected ***") 

    } 


func compareDate(dateInitial:Date, dateFinal:Date) -> Bool { 
     let order = Calendar.current.compare(dateInitial, to: dateFinal, toGranularity: .day) 
     switch order { 
     case .orderedSame: 
      return true 
     default: 
      return false 
     } 
    } 
+0

我想要這些日子,到下一個日期多長時間 – MMbach

2

試一試的Date這樣一個

let startDateComparisionResult:ComparisonResult = currentDate.compare(clickedDate! as NSDate) 

if startDateComparisionResult == ComparisonResult.orderedAscending 
{ 
    // Current date is smaller than end date. 
} 
else if startDateComparisionResult == ComparisonResult.orderedDescending 
{ 
    // Current date is greater than end date. 
} 
else if startDateComparisionResult == ComparisonResult.orderedSame 
{ 
    // Current date and end date are same 
} 
+0

哪一個更大或更小我在我的代碼中有 – MMbach

+0

取決於您當前的日期和clickedDate值 –

相關問題