我试图通过以下方式从NSDate和NSCalendar创建一个NSDateComponents:
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:[self.day intValue]];
[components setMonth:[self.month intValue]];
[components setYear:[self.year intValue]];
self.date = [calendar dateFromComponents:components];当日期、月份、年份为:9/31/2011 NSDate为10/1/2011。测试之后,我为NSCalendar设置了时区,没有改变NSDate。我也试过
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];给了我9/30/2011。有人看到我计算这个错误了吗?
谢谢。
发布于 2011-11-12 01:04:03
哦,9月31日真的是10月1日。
但是组件必须知道在哪个日历中工作。以下代码来自苹果
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];https://stackoverflow.com/questions/8101627
复制相似问题