将NSDate设置为特定日期,时间和时区

Str*_*rer 15 iphone xcode init nsdate

我需要将NSDate设置为iPhone App的特定日期,时间和时区.

下面的示例代码在iOS 4上运行正常,因为在iOS 4中引入了setTimeZone.如何在不使用setTimeZone的情况下轻松地将NSDate设置为日期,时间和时区,以便可以在iOS 3.0设备上使用?

    NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2010];
[comps setMonth:8];
[comps setDay:24];
[comps setHour:17];
[comps setMinute:5];
[comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] ];

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *referenceTime = [cal dateFromComponents:comps];
Run Code Online (Sandbox Code Playgroud)

猜猜这应该很简单,但不知何故NSDate,组件和日历和我不是好朋友......

Tho*_*ler 3

我想知道您是否可以在NSCalendar用于NSDateNSDateComponents. 我没有尝试过,也不知道它是否有效,但可能值得一试。