我有两个日期星期日和数据库中的条目的最后日期(核心数据)。现在我需要以数组形式创建日期列表。 数据库中包含字符串形式的开始日期和结束日期。
日期格式为MM / dd / yyyy。
答案 0 :(得分:11)
// minDate and maxDate represent your date range
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSInteger dayCount = 0;
while ( TRUE ) {
[days setDay: ++dayCount];
NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: minDate options: 0];
if ( [date compare: maxDate] == NSOrderedDescending )
break;
// Do something with date like add it to an array, etc.
}
[days release];
[gregorianCalendar release];
答案 1 :(得分:4)
我有NSCalendarUnit
更通用的方法来定义日期和日期之间的步骤。照顾正常化的日期。
iOS 8 API,Swift 2.0
func generateDates(calendarUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate) -> [NSDate] {
let calendar = NSCalendar.currentCalendar()
let normalizedStartDate = calendar.startOfDayForDate(startDate)
let normalizedEndDate = calendar.startOfDayForDate(endDate)
var dates = [normalizedStartDate]
var currentDate = normalizedStartDate
repeat {
currentDate = calendar.dateByAddingUnit(calendarUnit, value: 1, toDate: currentDate, options: NSCalendarOptions.MatchNextTime)!
dates.append(currentDate)
} while !calendar.isDate(currentDate, inSameDayAsDate: normalizedEndDate)
return dates
}
答案 2 :(得分:1)
通过将开始日期转换为Julian日(将生成浮点值),迭代到结束日期,并将迭代值从Julian天转换回NSDate对象,您可以非常轻松地完成此任务。
我在这个问题的答案(下面)中发布了一些方法,这些方法将提供必要的转换。
答案 3 :(得分:1)
这是Costique的快速解决方案,但有一点闪光。
func ...(lhs:NSDate, rhs:NSDate) -> [NSDate] {
var dates: [NSDate] = []
var cal = NSCalendar.currentCalendar() // or NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
var days = NSDateComponents()
var dayCount = 0
while true {
days.day = dayCount
let date:NSDate = cal.dateByAddingComponents(days, toDate: lhs, options: NSCalendarOptions.allZeros)!
if date.compare(rhs) == .OrderedDescending {
break
}
dayCount += 1
dates.append(date)
}
return dates
}
let fromDate = NSDate(timeIntervalSince1970: 1440711319)
let toDate = NSDate(timeIntervalSince1970: 1441316129)
fromDate...toDate // => ["Aug 27, 2015, 2:19 PM", "Aug 28, 2015, 2:19 PM", "Aug 29, 2015, 2:19 PM", "Aug 30, 2015, 2:19 PM", "Aug 31, 2015, 2:19 PM", "Sep 1, 2015, 2:19 PM", "Sep 2, 2015, 2:19 PM", "Sep 3, 2015, 2:19 PM"]
答案 4 :(得分:0)
Swift 3.0:
假设您想要从今天到接下来的60天获取日期数组。
extension Date {
func generateDates(startDate :Date?, addbyUnit:Calendar.Component, value : Int) -> [Date]
{
let calendar = Calendar.current
var datesArray: [Date] = [Date] ()
for i in 0 ... value {
if let newDate = calendar.date(byAdding: addbyUnit, value: i + 1, to: startDate!) {
datesArray.append(newDate)
}
}
return datesArray
}
}
<强>用法:强>
var datesArrayByAddingDays:[Date]?
override func viewWillAppear(_ animated: Bool) {
datesArrayByAddingDays = Date().generateDates(startDate: Date(), addbyUnit: .day, value: 60)
}
答案 5 :(得分:0)
@Nick Wargnier的Swift4版本:
func ...(lhs:Date, rhs:Date) -> [Date] {
var dates = [Date]()
let cal = NSCalendar.current
var days = DateComponents()
var dayCount = 0
while true {
days.day = dayCount
let date = cal.date(byAdding: days, to: lhs)!
if date.compare(rhs) == .orderedDescending {
break
}
dayCount += 1
dates.append(date)
}
return dates
}