我需要將NSDate設置爲iPhone應用程序的特定日期,時間和時區。將NSDate設置爲特定的日期,時間和時區
下面的示例代碼適用於iOS 4,因爲setTimeZone是在iOS 4中引入的。我怎樣才能輕鬆地將NSDate設置爲日期,時間和時區而不使用setTimeZone,因此可以在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];
猜測這應該是簡單的,但不知何故NSDate的,組件和日曆,我都沒有要好的朋友......
謝謝!我剛剛刪除了comps的setTimeZone併爲cal添加了一個,[cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@「UTC」]];它工作得很好,給了我相同的結果。大! – Structurer 2010-09-08 08:51:51