FSCalendar:如何获取两个日期中的日期?
FSCalendar: how to get dates in two dates?
我正在使用 Swift 3,我想打印两个日期之间的每一天。
例如:
08-10-2017 -> 开始日期
08-15-2017 -> 结束日期
应该打印:
08-10-2017
08-11-2017
08-12-2017
08-13-2017
08-14-2017
08-15-2017
我想获取两个特定日期的范围,请有人帮我。我试图将这两个日期放入 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)!
}
}
用法:
let today = Date()
let tenDaysLater = Calendar.current.date(byAdding: .day, value: 10, to: today)!
showRange(between: today, and: tenDaysLater)
我正在使用 Swift 3,我想打印两个日期之间的每一天。
例如:
08-10-2017 -> 开始日期
08-15-2017 -> 结束日期
应该打印:
08-10-2017
08-11-2017
08-12-2017
08-13-2017
08-14-2017
08-15-2017
我想获取两个特定日期的范围,请有人帮我。我试图将这两个日期放入 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)!
}
}
用法:
let today = Date()
let tenDaysLater = Calendar.current.date(byAdding: .day, value: 10, to: today)!
showRange(between: today, and: tenDaysLater)