2012-09-05 77 views
0

我試過下面的代碼片段我得到系統的當前日期和時間而不是選定的時區代碼。獲取所選時區的時間和日期?

NSDate *now = [NSDate date]; 
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]]; 

NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now]; 

NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateStyle:NSDateFormatterFullStyle]; 
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss "]; 
NSString *strSelectedDate= [formatter stringFromDate:selectedDate]; 
setZone.text = strSelectedDate;` 

任何想法?

回答

1

[NSDate date]返回表示當前日期和時間的日期對象,無論您身處何處。 NSDates不受地點或時區的限制。只有一個NSDate代表現在或任何其他時刻,而不是每個時區的不同日期對象。因此,您不應嘗試在時區之間轉換日期。

NSDate objects represent an absolute instant in time. Consider the following example of how two date representations in different time zones (9/9/11 3:54 PM in Paris and 9/9/11 11:54 PM in Sydney) are actually the same date. 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateStyle:NSDateFormatterShortStyle]; 
    [formatter setTimeStyle:NSDateFormatterShortStyle]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]]; 
    NSDate *aDate = [formatter dateFromString:@"9/9/11 3:54 PM"]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]]; 
    NSDate *anotherDate = [formatter dateFromString:@"9/9/11 11:54 PM"]; 
    NSLog(@"%@",anotherDate); 
    if ([aDate isEqualToDate:anotherDate]) { 
     NSLog(@"How about that?"); 
    } 

當涉及到輸出的日期,謹記的NSDate的描述方法GMT返回時間,你需要使用NSDateFormatter創建代表在巴黎,悉尼等。從本地時間日期字符串日期:

NSDate *now = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateStyle:NSDateFormatterShortStyle]; 
[formatter setTimeStyle:NSDateFormatterShortStyle]; 
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]]; 
NSLog(@"%@",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM 
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]]; 
NSLog(@"%@",[formatter stringFromDate:now]); //--> 9/9/11 3:54 PM 
+0

這是你想要的嗎? – prashant

+0

非常感謝,非常感謝 –

0
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"]; 

如果您不想顯示時區,請從格式字符串中刪除尾部'z'字符。

編輯

在另一方面,如果你只是想顯示的時區的名稱,只是使「Z」大寫。

編輯

小寫字母「Z」工作正常,所有其他時區,但不幸的是GMT是一個特例。所以最簡單的做法就是省略'z'並在格式化的日期附加「GMT」。

+0

我做了改變,但其顯示當前時間和系統的日期沒有選擇時區的時間或日期 –

+0

NSDateFormatter *格式化= [[NSDateFormatter的alloc] INIT]; [formatter setDateStyle:NSDateFormatterShortStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@「Europe/Paris」]]; NSDate * aDate = [formatter dateFromString:@「9/9/11 3:54 PM」]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@「Australia/Sydney」]]; NSDate * anotherDate = [formatter dateFromString:@「9/9/11 11:54 PM」]; NSLog(@「%@」,anotherDate);如果([aDate isEqualToDate:anotherDate]){ NSLog(@「那怎麼樣?」); } – prashant