Reputation: 2777
I want to get date which is the 3rd Saturday of the month between two dates. I have done but it's not what i want.
NSInteger count = 0;
NSInteger saturday = 7;
// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];
// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];
if (dateComponents.weekday == saturday) {
count++;
NSLog(@"Date = %@",currentDate);
}
// "Increment" currentDate by one day.
currentDate = [calendar dateByAddingComponents:oneDay
toDate:currentDate
options:0];
}
In my code i have successfully get Saturday but it's all Saturdays get which is in between startdate
& enddate
.
I have also try this :
[dateComponents setWeekdayOrdinal:3]; but not working.
This is my log
2016-06-17 11:29:18.319 Floacstation[1715:84848] Current Date :2016-06-16 18:30:00 +0000
2016-06-17 11:29:18.319 Floacstation[1715:84848] To Date : 2016-08-16 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-19 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-26 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-03 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-10 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-17 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-24 18:30:00 +0000
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-31 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-07 18:30:00 +0000
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-14 18:30:00 +0000
Upvotes: 1
Views: 139
Reputation: 2251
NSInteger count = 0;
NSInteger saturday = 7;
// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];
// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];
if (dateComponents.weekday == saturday) {
count++;
NSLog(@"3rd Satureday Date = %@",currentDate);
if(count == 3)
break;
}
// "Increment" currentDate by one day.
currentDate = [calendar dateByAddingComponents:oneDay
toDate:currentDate
options:0];
}
This is my log
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-17 18:30:00 +0000
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-24 18:30:00 +0000
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-01 18:30:00 +0000
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-08 18:30:00 +0000
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-15 18:30:00 +0000
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-22 18:30:00 +0000
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-29 18:30:00 +0000
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-05 18:30:00 +0000
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-12 18:30:00 +0000
Upvotes: 0
Reputation: 285092
NSCalendar
has powerful skills to do that without repeat loops
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 7;
components.weekdayOrdinal = 3;
components.month = [calendar component:NSCalendarUnitMonth fromDate:currentDate];
NSDate *nextThirdSaturday = [calendar nextDateAfterDate:currentDate matchingComponents: components options: NSCalendarMatchNextTime];
if (nextThirdSaturday && [nextThirdSaturday compare:toDate] == NSOrderedAscending) {
NSLog(@"%@", nextThirdSaturday);
} else {
NSLog(@"Not Found");
}
Edit:
If you want the 3rd saturday calculated from the start date and within the range of start and end date use this
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 7;
__block NSInteger count = 1;
__block NSDate *found;
[calendar enumerateDatesStartingAfterDate:currentDate matchingComponents:components options:NSCalendarMatchNextTime usingBlock:^(NSDate *date, BOOL exactMatch, BOOL *stop) {
if (count == 3) { found = date; }
if (found || [date compare:toDate] == NSOrderedDescending) *stop = YES;
count++;
}];
NSLog(@"%@", found);
Upvotes: 1
Reputation: 880
Reason:
Notice that you have entered date 17th
but its returning you 16th
.. As it is considering UTC
format whenever you are printing NSDate in NSLog
or converting it explicitly to NSString
.
Whenever You want the date in string Then Use
NSDateFormatter
as i have used in this Code..
The problem you are facing is just because of that.
so, Wherever you have used Date, just replace the method of converting date
into String & check again for Output..
Try this one... It must give you proper answer...
NSInteger count = 0;
NSInteger saturday = 7;
// Set the incremental interval for each interaction.
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
// Using a Gregorian calendar.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"];
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"];
// Iterate from fromDate until toDate
while ([currentDate compare:toDate] == NSOrderedAscending) {
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
[dateComponents setWeekdayOrdinal:3];
if (dateComponents.weekday == saturday) {
count++;
if (count==3) {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
formatter.dateFormat=@"dd MM YYYY";
NSLog(@"Date = %@",[formatter stringFromDate:currentDate]);
}
}
}
// There's no need to Increment this Date..
// "Increment" currentDate by one day.
// currentDate = [calendar dateByAddingComponents:oneDay
// toDate:currentDate
// options:0];
Hope this helps... :)
Upvotes: 3
Reputation: 542
According to my understanding, the date you want is 2016-07-02
?
Try this:
NSDate *currentDate = [NSDate dateWithString:@"2016-06-16" formatString:@"yyyy-MM-dd"];
NSDate *toDate = [NSDate dateWithString:@"2016-08-17" formatString:@"yyyy-MM-dd"];
NSLog(@"Oh, %@ is the 3rd Saturday", [[currentDate dateByAddingWeeks:2] dateByAddingDays:(saturday - currentDate.weekday + 1)]);
I use a 3rd library called DateTools.
Upvotes: 0