2017-08-15 40 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的快速答案,其现在的工作,感谢的人! –

相关问题