NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy";
NSDate *date = [dateFormatter dateFromString:@"2011"];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[oneDayAgoComponents setMonth:0];
NSUInteger numWeekdays = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
int w = numWeekdays;
for(int currentdateindexpath=0;currentdateindexpath<=12;currentdateindexpath++)
{
[formatter setDateFormat:@"MMMM yyyy"];
[oneDayAgoComponents setMonth:currentdateindexpath];
NSDate *monthAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date
options:0];
NSString *montYear = [formatter stringFromDate:monthAgo];
NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit
fromDate:date];
[subcomponent setWeek:currentdateindexpath];
[formatter setDateFormat:@"'week'W"];
NSString *stringFromDate = [formatter stringFromDate:monthAgo];
// Get the weekday component of the current date
[oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date options:0];
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
NSDateComponents *components =
[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit) fromDate: beginningOfWeek];
beginningOfWeek = [calendar dateFromComponents:components];
for (int d = 0; d <= w; d++)
{
NSDate *newDate1 = [beginningOfWeek dateByAddingTimeInterval:60*60*24*d];
NSLog(@"days %@",newDate1);
//+++++++++++++++++++ start week+++++++++++++++
}
[oneDayAgoComponents setDay:7 - ([subcomponent weekday])];
NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date options:0];
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
NSLog(@"...>%@",[NSString stringWithFormat:@"%@ %@ to %@ %@", stringFromDate, weekstartdate, weekendtdate, montYear]);
}
if i pass the year as 2011 i need to get the month,week,day list. In the corresponding month how many weeks available in that particular week how many days available and from where the week start and end date need to display..but in this code each and every month of the first week only displaying,i need the remaining week of the month...
days 2010-12-25 18:30:00 +0000
days 2010-12-26 18:30:00 +0000
days 2010-12-28 18:30:00 +0000
days 2010-12-29 18:30:00 +0000
days 2010-12-30 18:30:00 +0000
days 2010-12-31 18:30:00 +0000
days 2011-01-01 18:30:00 +0000
...>week1 26/12/10 to01/01/11 January 2011
days 2011-01-25 18:30:00 +0000
days 2011-01-26 18:30:00 +0000
days 2011-01-27 18:30:00 +0000
days 2011-01-28 18:30:00 +0000
days 2011-01-29 18:30:00 +0000
days 2011-01-30 18:30:00 +0000
days 2011-01-31 18:30:00 +0000
days 2011-02-01 18:30:00 +0000
...>week1 26/01/11 to01/02/11 February 2011
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy";
NSDate *date = [formatter dateFromString:@"2011"];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
[oneDayAgoComponents setMonth:0];
NSUInteger numWeekdays = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
int w=numWeekdays;
for(int currentdateindexpath=0;currentdateindexpath<=52;currentdateindexpath++)
{
[oneDayAgoComponents setWeek:currentdateindexpath];
NSDate *monthAgo = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date
options:0];
[formatter setDateFormat:@"MMMM yyyy"];
NSString *montYear = [formatter stringFromDate:monthAgo];
[formatter setDateFormat:@"'week'W"];
stringFromDate = [formatter stringFromDate:monthAgo];
NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit
fromDate:date];
[oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
//NSLog(@"oneDayAgoComponents1 %@",oneDayAgoComponents);
NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date options:0];
//NSLog(@" beginningOfWeek %@",beginningOfWeek);
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
// NSLog(@"weekstartdate %@",weekstartdate);
NSDateComponents *components =
[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit) fromDate: beginningOfWeek];
beginningOfWeek = [calendar dateFromComponents:components];
for(int d=0;d<=w;d++){
NSDate *newDate1 = [beginningOfWeek dateByAddingTimeInterval:60*60*24*d];
NSString *date_ = [formatter stringFromDate:newDate1];
NSLog(@"days %@",date_);
//NSLog(@"Show %@",beginningOfWeek);
}
[oneDayAgoComponents setDay:7- ([subcomponent weekday])];
// NSLog(@"oneDayAgoComponents2 %@",oneDayAgoComponents);
NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
toDate:date options:0];
// NSLog(@" endOfWeek %@",endOfWeek);
[formatter setDateFormat:@"dd/MM/yy"];
NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
// NSLog(@"weekendtdate %@",weekendtdate);
NSLog(@"...>%@",[NSString stringWithFormat:@"%@ %@ to%@ %@ ",stringFromDate,weekstartdate,weekendtdate,montYear]);
}
You could do something like this:
NSString *userYearString = @"2011";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Get the year from the string
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy";
NSDate *startOfTheYear = [dateFormatter dateFromString:userYearString];
// Loop until the year is over printing the data you need
//
// Some objects we'll use in the loop and that's better to instantiate only once
NSDateFormatter *weekIntervalFormatter = [[NSDateFormatter alloc] init];
weekIntervalFormatter.dateFormat = @"dd/MM/yy";
NSDateFormatter *monthFormatter = [[NSDateFormatter alloc] init];
monthFormatter.dateFormat = @"MMMM yyyy";
NSDate *currentDate = startOfTheYear;
NSTimeInterval oneDayTimeInterval = 86400;
// We'll use userYear to determing when to stop the loop
NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:currentDate];
NSInteger userYear = [components year];
do {
// Get the month at the begin of the iteration, to use it as the stop comparator
NSInteger previousMonth = [[calendar components:NSCalendarUnitMonth fromDate:currentDate] month];
// Evaluate the month string once at the begin of the iteration
NSString *monthString = [monthFormatter stringFromDate:currentDate];
// Move a the beginning of the month
NSDate *startOfMonthDate;
NSTimeInterval _interval;
[calendar rangeOfUnit:NSMonthCalendarUnit
startDate:&startOfMonthDate
interval:&_interval
forDate:currentDate];
// From the beginning of the month move at the beginning of the week
NSDate *startOfWeekDate;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfWeekDate
interval:&_interval
forDate:startOfMonthDate];
currentDate = startOfWeekDate;
// Loop printing the days and week information until the month is over
NSUInteger weekCounter = 1;
do {
// print a the days of the current week
NSDate *weekStartDate = currentDate;
for (int i=0; i < 7; i++) {
NSLog(@"day %@", currentDate);
currentDate = [currentDate dateByAddingTimeInterval:oneDayTimeInterval];
}
NSDate *weekEndDate = [currentDate dateByAddingTimeInterval:-oneDayTimeInterval];
// prepare the message using the date formatter previously created
NSString *formattedStartDay = [weekIntervalFormatter stringFromDate:weekStartDate];
NSString *formattedEndDay = [weekIntervalFormatter stringFromDate:weekEndDate];
NSString *message = [NSString stringWithFormat:@"...>week %d, %@ to %@\t%@", weekCounter, formattedStartDay, formattedEndDay, monthString];
NSLog(@"%@", message);
weekCounter++;
// evalutate the end of loop condition
// = is the month over?
components = [calendar components:NSCalendarUnitMonth fromDate:currentDate];
} while ([components month] == previousMonth); // loop until the month is over
// evaluate the end of loop condition
// = is the year over?
components = [calendar components:NSCalendarUnitYear fromDate:currentDate];
} while ([components year] == userYear); // loop until the year is over
This version gives this output
day 2010-12-26 00:00:00 +0000
day 2010-12-27 00:00:00 +0000
day 2010-12-28 00:00:00 +0000
day 2010-12-29 00:00:00 +0000
day 2010-12-30 00:00:00 +0000
day 2010-12-31 00:00:00 +0000
day 2011-01-01 00:00:00 +0000
...>week 1, 26/12/10 to 01/01/11 January 2011
day 2011-01-02 00:00:00 +0000
day 2011-01-03 00:00:00 +0000
day 2011-01-04 00:00:00 +0000
day 2011-01-05 00:00:00 +0000
day 2011-01-06 00:00:00 +0000
day 2011-01-07 00:00:00 +0000
day 2011-01-08 00:00:00 +0000
...>week 2, 02/01/11 to 08/01/11 January 2011
day 2011-01-09 00:00:00 +0000
day 2011-01-10 00:00:00 +0000
day 2011-01-11 00:00:00 +0000
day 2011-01-12 00:00:00 +0000
day 2011-01-13 00:00:00 +0000
day 2011-01-14 00:00:00 +0000
day 2011-01-15 00:00:00 +0000
...>week 3, 09/01/11 to 15/01/11 January 2011
day 2011-01-16 00:00:00 +0000
day 2011-01-17 00:00:00 +0000
day 2011-01-18 00:00:00 +0000
day 2011-01-19 00:00:00 +0000
day 2011-01-20 00:00:00 +0000
day 2011-01-21 00:00:00 +0000
day 2011-01-22 00:00:00 +0000
...>week 4, 16/01/11 to 22/01/11 January 2011
day 2011-01-23 00:00:00 +0000
day 2011-01-24 00:00:00 +0000
ay 2011-01-25 00:00:00 +0000
day 2011-01-26 00:00:00 +0000
day 2011-01-27 00:00:00 +0000
day 2011-01-28 00:00:00 +0000
day 2011-01-29 00:00:00 +0000
...>week 5, 23/01/11 to 29/01/11 January 2011
day 2011-01-30 00:00:00 +0000
day 2011-01-31 00:00:00 +0000
day 2011-02-01 00:00:00 +0000
day 2011-02-02 00:00:00 +0000
day 2011-02-03 00:00:00 +0000
day 2011-02-04 00:00:00 +0000
day 2011-02-05 00:00:00 +0000
...>week 6, 30/01/11 to 05/02/11 January 2011
day 2011-01-30 00:00:00 +0000
day 2011-01-31 00:00:00 +0000
day 2011-02-01 00:00:00 +0000
day 2011-02-02 00:00:00 +0000
day 2011-02-03 00:00:00 +0000
day 2011-02-04 00:00:00 +0000
day 2011-02-05 00:00:00 +0000
...>week 1, 30/01/11 to 05/02/11 February 2011
day 2011-02-06 00:00:00 +0000
day 2011-02-07 00:00:00 +0000
day 2011-02-08 00:00:00 +0000
day 2011-02-09 00:00:00 +0000
day 2011-02-10 00:00:00 +0000
day 2011-02-11 00:00:00 +0000
day 2011-02-12 00:00:00 +0000
...>week 2, 06/02/11 to 12/02/11 February 2011
...
As you can see I've used a different implementation from yours... I prefer the do-while
approach to understand when the month or year is over, as it's more introspective. I also tried to create as few objects as possible, which is always a good idea.
I'd also recommend you this book: Clean Code, It's full of techniques and tips to write effective code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With