ios - NSDate 问题,日期应该是前一天

标签 ios ios4 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];

最佳答案

您的时区似乎类似于 GMT-3(或 GMT+3,我不记得了)。使用 GMT 代替 systemTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 它可以解决您的问题。

关于ios - NSDate 问题,日期应该是前一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519710/

相关文章:

ios - 如何在 iOS View 中添加虚线

iphone - 如何使用 UIImageView 和 UILabel 创建 UIBarButtonItem

iphone - 使用 UIRefreshControl

ios - 如何使用 segues 将字典数据从一个 View Controller 发送到另一个 View Controller ?

iphone - iOS : Storyboard backwards compatible

ios - 将持续数天的 NSDate 拆分为几个跨度

ios - 将 ISO8601 日期格式转换为本地时间 (iOS)

iOS 7 在企业应用程序中访问 UUID 值(不适用于 AppStore)

iphone - MapKit:如何平移和缩放以适合我的所有注释?

ios - NSDateFormatter 在设置日期时没有给出正确的响应