NSDate问题,日期应该是前一天
我想实现方法,该方法将计算一个月的天数。一切都很好,除了我的约会是前一天。在以下示例中,日期为“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];
I want to implement method, that will count the number of days in month. Everything is fine except my date is the day before then it should be. In the following example date is "2012-09-14 21:00:00 +0000" and should be 2012-09-15 21:00:00 +0000 Will appreciate for your help. My code is:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的时区似乎是 GMT-3(或 GMT+3,我不记得了)。使用 GMT 代替
systemTimeZone
:[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
它可以解决您的问题。Your timeZone seems to be something like GMT-3 (or GMT+3, I never remember). Instead of the
systemTimeZone
use the GMT one:[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
and it solves your problem.