2017-08-15 35 views
3

我正在使用Swift 3,並且我想在兩個日期之間打印每一天。FSCalendar:如何在兩個日期中獲取日期?

例如:

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日

我想在兩個範圍具體日期,請有人幫助我。我試圖把這兩個日期進行循環,但沒有機會。

回答

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的快速答案,其現在的工作,感謝的人! –

相關問題