2017-08-15 53 views
3

私はSwift 3を使用しています。私は2つの日付の間に毎日を印刷したいと思います。例えばFSCalendar:2つの日付で日付を取得するにはどうすればよいですか?

2017年8月10日 - >開始日

2017年8月15日 - >終了日

は印刷する必要があります:

を08-10-2017

08-11-2 017

2017年8月12日

2017年8月13日

2017年8月14日

2017年8月15日

私は二つに範囲を取得したいです誰かが私を助けてくれますか?私はこれらの2つの日付をforループに入れようとしましたが、チャンスはありません。

答えて

3

カレンダーベースの日付を作成し、終了日に達するまで開始日を増やす必要があります。ここでは、コードスニペットはそれを行う方法を、次のとおりです。

func showRange(between startDate: Date, and endDate: Date) { 
    // Make sure startDate is smaller, than endDate 
    guard startDate < endDate else { return } 

    // Get the current calendar, i think in your case it should some fscalendar instance 
    let calendar = Calendar.current 
    // Calculate the endDate for your current calendar 
    let calendarEndDate = calendar.startOfDay(for: endDate) 

    // Lets create a variable, what we can increase day by day 
    var currentDate = calendar.startOfDay(for: startDate) 

    // Run a loop until we reach the end date 
    while(currentDate <= calendarEndDate) { 
     // Print the current date 
     print(currentDate) 
     // Add one day at the time 
     currentDate = Calendar.current.date(byAdding: .day, value: 1, to: currentDate)!  
    } 
} 

用途:

let today = Date() 
let tenDaysLater = Calendar.current.date(byAdding: .day, value: 10, to: today)! 
showRange(between: today, and: tenDaysLater) 
+0

おかげで迅速な答え、その今働いて、感謝の人のために@dirtydanee! –

関連する問題