2012-08-04 90 views
1

如何將GMT轉換爲IOS中的IST?我有代碼,這將使我在格林威治標準時間的時間。但我想將其轉換爲相應的本地時區,並在我的應用程序中使用它。我怎樣才能做到這一點 ? 在此先感謝。在iOS中將GMT轉換爲IST

回答

8

下面的代碼將GMT轉換爲IST。

NSString *inDateStr = @"2000/01/02 03:04:05"; 
NSString *s = @"yyyy/MM/dd HH:mm:ss"; 

// about input date(GMT) 
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init]; 
inDateFormatter.dateFormat = s; 
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
NSDate *inDate = [inDateFormatter dateFromString:inDateStr]; 

// about output date(IST) 
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init]; 
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"]; 
outDateFormatter.dateFormat = s; 
NSString *outDateStr = [outDateFormatter stringFromDate:inDate]; 

// final output 
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr); 
1
[(NSDateFormatter *) setTimeZone:[NSTimeZone localTimeZone]]; this will give u the local conversion of the GMT to local time zone. 
1

我用下面的代碼將GMT轉換爲IST。

NSTimeZone *zone1=[NSTimeZone localTimeZone]; 
long second=[zone1 secondsFromGMT]; //offset 
NSDate *currentdate=[NSDate date]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MMddyyyy hh:mm:ss"]; // format 
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:second]]; 

dateStr = [dateFormat stringFromDate:currentdate]; 
NSLog(@"Current date %@",dateStr); 

推薦給您的朋友,

謝謝