-(NSDate*)convertThisDate:(NSDate*)aDate
toThisTimeZone:(NSString*)timeZoneAbbreviation{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *yourDate = [NSDate date];
NSString *yourDateAsString = [dateFormatter stringFromDate:yourDate];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:timeZoneAbbreviation]];
NSDate *convertedDate = [dateFormatter dateFromString:yourDateAsString];
NSLog(@"convertedDate : %@",convertedDate);
return convertedDate;
}
//For Local Timezone
-(NSDate*)convertThisDateToLocalTimeZone:(NSDate*)aDate{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *yourDate = [NSDate date];
NSString *yourDateAsString = [dateFormatter stringFromDate:yourDate];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//current local time zone in device
NSDate *convertedDate = [dateFormatter dateFromString:yourDateAsString];
NSLog(@"convertedDate : %@",convertedDate);
return convertedDate;
}
用法:
NSDate *UTCDate = [self convertThisDate:myDate
toThisTimeZone:@"UTC"];
NSDate *GMTDate = [self convertThisDate:myDate
toThisTimeZone:@"GMT"];
NSDate *ESTDate = [self convertThisDate:myDate
toThisTimeZone:@"EST"];
//For Local Timezone
NSDate *localTimeZoneDate = [self convertThisDateToLocalTimeZone:myDate];
要獲得支持縮寫的列表,你可以做的NSLog時區列表,
NSLog(@"TimeZone List: %@", [NSTimeZone abbreviationDictionary]);
TimeZone List: {
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
}
我需要將本地時間轉換爲另一個時區而不更改時間2014-12-20 16:30:00 GMT + 5:30和2014-12-20 16:30:00 GMT + 2 :30然後轉到utc – Vinoth 2014-12-19 12:00:25