Ios FSCalendar:如何在两个日期中获取日期?
我使用的是Swift 3,我想在两个日期之间每天打印 例如: 2017年10月8日->开始日期 2017年8月15日->结束日期 应打印: 08-10-2017 08-11-2017 08-12-2017 08-13-2017 2017年8月14日 08-15-2017Ios FSCalendar:如何在两个日期中获取日期?,ios,swift,fscalendar,Ios,Swift,Fscalendar,我使用的是Swift 3,我想在两个日期之间每天打印 例如: 2017年10月8日->开始日期 2017年8月15日->结束日期 应打印: 08-10-2017 08-11-2017 08-12-2017 08-13-2017 2017年8月14日 08-15-2017 我想得到两个具体日期的范围,有人能帮我吗。我尝试将这两个日期放入for循环,但没有机会。您需要创建一个基于日历的日期,并开始增加开始日期,直到达到结束日期。下面是一段代码片段,介绍如何执行此操作: func showRange(
我想得到两个具体日期的范围,有人能帮我吗。我尝试将这两个日期放入for循环,但没有机会。您需要创建一个基于日历的日期,并开始增加开始日期,直到达到结束日期。下面是一段代码片段,介绍如何执行此操作:
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)!
}
}
谢谢@dirtydanee的快速回答,它现在可以工作了,谢谢你!。
let today = Date()
let tenDaysLater = Calendar.current.date(byAdding: .day, value: 10, to: today)!
showRange(between: today, and: tenDaysLater)