2012-03-21 66 views
0

我必須在當地時間下午6點設置UIAlertNotification。時間設定在上午11點,因爲我現在所處的當地時區是格林威治標準時間+6:00,因此時間爲下午18點。但是,在不同的時區它會有所不同。我希望在任何時區下午六點。任何人都可以幫助我嗎?謝謝。關於NSDateComponents TimeZone

NSDateComponents *comps=[[[NSDateComponents alloc]init]autorelease]; 
    [comps setYear:[year intValue]]; 
    [comps setMonth:[month intValue]]; 
    [comps setDay:[day intValue]]; 
    [comps setHour:[@"11" intValue]];//6pm 
    [comps setMinute:0]; 
    [comps setMinute:0]; 
    [comps setTimeZone:[NSTimeZone localTimeZone]]; 
    NSCalendar *cal=[[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    NSDate *date1=[[cal dateFromComponents:comps]retain]; 

//本地警報代碼

NSInteger minutesAhead = 0; 
    NSTimeInterval seconds = (60 * minutesAhead) * -1; 
    NSDate *actualFireDate = [date1 dateByAddingTimeInterval:seconds]; 
    NSMutableDictionary *dictC = [NSMutableDictionary dictionaryWithDictionary:userInfo]; 
    [dictC setObject:date1 forKey:@"PCOriginalReminderDate"]; 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.fireDate = actualFireDate; 
    notification.alertBody = message; 
    notification.userInfo = dictC; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    [notification release]; 

注:

當我打印的日期,它說:

date = "2012-04-04 18:00:00 +0000"; 

我認爲這意味着,上午11點是GMT和通過增加7小時(自格林威治標準時間+7 - 日光以來),並使其18點。

回答

1

你要設置你的UILocalNotificationtimeZone property

在fireDate指定的日期爲根據此屬性的值解釋。如果您指定nil(默認值),則會將啓動日期解釋爲絕對GMT時間,適用於倒數計時器等情況。如果您爲此屬性指定了有效的NSTimeZone對象,則會將啓用日期解釋爲在時區發生更改時自動調整的掛鐘時間;適合這種情況的例子是鬧鐘。

因此,如果您設置通知的fireDate你在你的代碼段創建,並設置其timeZone[NSTimeZone localTimeZone]那麼你會好起來的時間。

+0

我想我做的一切都是對的。我已添加完整的代碼。請讓我知道我犯了什麼錯誤。 – Ahsan 2012-03-22 00:44:58

+0

當你說「當我打印日期」時,你在說什麼日期?另外,你沒有設置通知的'timeZone'屬性。 – yuji 2012-03-22 08:06:58