iphone – 將NSDate設定為特定的日期,時間和時區
我需要將NSDate設定為iPhone App的特定日期,時間和時區.
下面的示例程式碼在iOS 4上可以正常工作,因為setTimeZone與iOS 4一起引入.如何在不使用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];
猜猜這應該很簡單,但不知何故NSDate,元件和日曆和我不是好朋友…
我想知道是否可以在NSCateendar物件上設定時區,用於從NSDateComponents建立NSDate.我沒有嘗試過這個,不知道是否可以工作,但這可能值得一試.
http://stackoverflow.com/questions/3655616/setting-an-nsdate-to-specific-date-time-and-timezone