Kamlesh Shingarakhiya Kamlesh Shingarakhiya - 5 months ago 9
Objective-C Question

Get 3rd Saturday of the month between startdate and enddate

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

Answer

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... :)

Comments