NSCalendar 和 NSDateComponents 返回意外日期
我正在尝试从 NSCalendar 和 NSDateComponents 创建一个 NSDate:
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。有人发现我的计算方式有错误吗?
谢谢。
I'm trying to create an NSDate from NSCalendar and NSDateComponents with:
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];
When the day, month, year are : 9/31/2011 the NSDate is for 10/1/2011. I set the timezone for the NSCalendar after testing which didn't change the NSDate. I also tried
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
Which gave me back 9/30/2011. Anyone see an error in the way I'm calculating this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,9 月 31 日确实是 10 月 1 日。
但组件必须知道在哪个日历中工作。以下代码来自苹果
Well, september 31 is really october first.
But a component has to know in which calendar to work. The following code is from apple