2016-11-18 5 views
7

Swift 3で2つの日付を比較するにはどうすればよいですか? 他のSwiftバージョンの多くのソリューションを見つけましたが、私のためには機能しません。Swift 3で2つの日付(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 
    } 
} 

は今、あなたがこのような文字列を日付に変換するにはDateFormatterを使用することができ、この

let numOfDays = fromDate.daysBetweenDate(toDate: toDate) 

のようにこの拡張機能を使用しています。

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

私はあなたをよく理解しているのか分かりません。私の答えは、your_dateとDate()(今日)の間の日数を示します。しかし、別の日付と比較したい場合は、Date()をnext_dateに変更してください。 –

+0

はい、それは私が欲しいものです。しかし、私は数がない日に、私はどのように番号を取得するのですか? – MMbach

2

使用この機能を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

あなたのcurrentDateとclickedDateの値によって異なります –

関連する問題