문제

I am trying to get the start and end dates of the current month. There are a few similar questions here, but I am getting unexpected results while using them. Here's what I did:

NSDate *monthStart,*monthEnd;   
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];          
NSDateComponents *comps = [cal components:NSDayCalendarUnit fromDate:[NSDate date]];
[comps setDay:1];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
monthStart = [cal dateFromComponents:comps];
[comps setDay:[[NSCalendar currentCalendar]
               rangeOfUnit:NSDayCalendarUnit
               inUnit:NSMonthCalendarUnit
               forDate:[NSDate date]].length];
[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];
monthEnd = [cal dateFromComponents:comps];
NSLog(@"%@",monthStart);
NSLog(@"%@",monthEnd);

Unfortunately the output is :

2012-12-24 15:39:28.159 Popup[1010:403] 0001-12-31 18:06:32 +0000
2012-12-24 15:39:28.160 Popup[1010:403] 0001-01-31 18:06:31 +0000

BTW, the timezone configured in my machine is +5:30

도움이 되었습니까?

해결책

I have modified your code little, to get the correct answer

NSDate *monthStart,*monthEnd;
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate];
[comps setDay:1];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:19800]];
monthStart = [cal dateFromComponents:comps];


[comps setDay:[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]].length];
[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];
monthEnd = [cal dateFromComponents:comps];

NSLog(@"Start %@",monthStart);
NSLog(@"End   %@",monthEnd);

다른 팁

If you want to know the starting and ending date for current month...

Starting is always 1st, and ending can be calculated by month name!!!

Then it is very simple date arithmetic,

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

NSMutableArray *months=[NSMutableArray arrayWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec", nil];
NSMutableArray *days=[NSMutableArray arrayWithObjects:@"31",@"28",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
//check if leap
[dateFormat setDateFormat:@"YYYY"];
NSInteger year=[[dateFormat stringFromDate:[NSDate date]]integerValue];
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
    [days replaceObjectAtIndex:1 withObject:@"29"];
}

NSMutableDictionary *daysInMonthDict=[[NSMutableDictionary alloc]initWithObjects:days forKeys:months];

[dateFormat setDateFormat:@"MMM"];
NSLog(@"Starting date is 1 and ending date is %@",[daysInMonthDict objectForKey:[dateFormat stringFromDate:[NSDate date]]]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top