我有两个日期StartDate和EndDate。和周日,我想找到特定的日子,例如这两个日期之间的周一,周二,周六。以及如何在第二次循环中更改特定日期。
我想在数组流中添加多个Object下面的代码
NSDictionary *dict = @{
@"name": @"kiran",
@"startdate": @"25-11-16",
@"SUNDAY": @"none"
@"MONDAY": @"monday"
@"TUESDAY": @"tuesday"
@"WEDNESDAY": @"none"
@"THURSDAY": @"none"
@"FRIDAY": @"friday"
@"SATURDAY": @"none"
@"EndDate": @"7-12-2016"
};
NSDictionary *dict1 = @{
@"name": @"Bala",
@"startdate": @"1-11-16",
@"SUNDAY": @"sunday"
@"MONDAY": @"none"
@"TUESDAY": @"tuesday"
@"WEDNESDAY": @"none"
@"THURSDAY": @"thursday"
@"FRIDAY": @"friday"
@"SATURDAY": @"none"
@"EndDate": @"7-12-2016"
};
NSDictionary *dict2 = @{
@"name": @"Sri",
@"startdate": @"1-12-16",
@"SUNDAY": @"sunday"
@"MONDAY": @"monday"
@"TUESDAY": @"tuesday"
@"WEDNESDAY": @"wednesday"
@"THURSDAY": @"thursday"
@"FRIDAY": @"friday"
@"SATURDAY": @"saturday"
@"EndDate": @"7-12-2016"
};
NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:dict,dict1,dict2, nil];
如何在两个日期之间执行循环?
我想输出的是:第一个字典
25-11-2016,星期五,kiran
2016年11月28日,星期一,kiran
2016年11月29日,星期二,kiran
2016年12月12日星期五,kiran
2016年12月5日星期一,kiran
2016年12月6日星期二,kiran
与第二和第三个dict输出相同。并且所有工作日都存储在一个阵列中。
答案 0 :(得分:0)
extension Date {
static func generateDatesArrayBetweenTwoDates(startDate: Date, endDate: Date) -> [Date] {
var datesArray: [Date] = [Date]()
var startDate = startDate
let calendar = Calendar.current
while startDate <= endDate {
let tempDate = self.dateFormatter().string(from: startDate)
datesArray.append(self.dateFormatter().date(from: tempDate) ?? Date())
startDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
}
return datesArray
}
static func dateFormatter() -> DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
return formatter
}
}
func getDates() {
var count = 0
let tempStartDate = Date.dateFormatter().string(from: Date())
guard let startDate = Date.dateFormatter().date(from: tempStartDate) else {
return
}
let tempEndDate = Date.dateFormatter().string(from: Date(timeIntervalSince1970: 1575181456)) // lets say 1st dec 2019
guard let endDate = Date.dateFormatter().date(from: tempEndDate) else {
return
}
let datesArray = Date.generateDatesArrayBetweenTwoDates(startDate: endDate, endDate: startDate)
for date in datesArray {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents([.weekday], from: date)
if components.weekday == 6 { // For friday check
count += 1
print("Hello Friday")
} else {
print("It's not Friday")
}
}
print("number of fridays", count)
}
希望有帮助
答案 1 :(得分:-1)
我更习惯swift所以我将使用swift而不是objC给出我的答案,如果你必须使用objC作为代码,请将其读作伪代码
获得你想要的结果只是说MutableArray的第一项
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dict = listMutableArray[0] as Dictionary
let name = dict["name"]
let startDate = dateFormatter.date(from: dict["startdate"])
let endDate = dateFormatter.date(from: dict["enddate"])
let totalDuration = endDate.timeIntervalSince(startDate)
let totalDuration = totalDuration/86400 //to get days from seconds - 86400 = 60*60*24
let weekDaysToCount:Array<Bool> = Array()
if dict["MONDAY"] != "none" {
weekDaysToCount.append(true)
} else {
weekDaysToCount.append(false)
}
... ...
//Repeat for all days
for i in 0..<Int(totalDuration) {
for validDay in weekDaysToCount {
startDate = startDate.addingTimeInterval(86400) //one day
if validDay {
print("\(startDate), \(name)")
}
}
}
请注意,weekDaysToCount假设开始日是星期一,当情况不是这样时,您需要调整数组。