我想实现方法,它将计算月中的天数。一切都很好,除了我的日期是前一天它应该是。在以下示例中,日期为“2012-09-14 21:00:00 +0000”,应该是2012-09-15 21:00:00 +0000将感谢您的帮助。我的代码是:
NSDictionary *curDay = [NSDictionary dictionaryWithObjectsAndKeys:
@"9",@"mounthNumber",@"15",@"day",@"2012",@"year", nil];
NSDateComponents *selectedMounth = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *calendar = [[[NSCalendar alloc]initWithCalendarIdentifier:
NSGregorianCalendar] autorelease];
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
[calendar setTimeZone: timezone];
[selectedMounth setYear:[[curDay objectForKey:@"year"] integerValue]];
[selectedMounth setMonth:[[curDay objectForKey:@"mounthNumber"] integerValue]];
[selectedMounth setDay:[[curDay objectForKey:@"day"] integerValue]];
NSDate *date = [calendar dateFromComponents:selectedMounth];
答案 0 :(得分:4)
你的时间区似乎是GMT-3(或GMT + 3,我永远不记得)。而不是systemTimeZone
使用GMT:[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
它可以解决您的问题。