我的时间本地时间字符串 2014-12-20 16:30:00 GMT + 5:30 ,我需要将同一时间转换为不同的区域 2014-12 -20 16:30:00 GMT + 2:30 然后想要同时转换 2014-12-20 16:30:00 GMT + 2:3 0到UTC为< strong> 2014-12-20 14:00:00 as string。
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
df2.dateStyle = NSDateFormatterMediumStyle;
[df2 setDateFormat:@"YYYY-MM-dd HH:mm:ss z"];
[df2 setTimeZone:[NSTimeZone timeZoneWithName:timezonename]];
start_date=[df2 stringFromDate:datepick.date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss z'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:timezonename]];
NSDate *dte = [dateFormat dateFromString:start_date];
NSLog(@"Date: %@--%@",start_date, dte);
请帮忙...
提前致谢..
答案 0 :(得分:2)
您可以使用此代码。可能对你有帮助。
NSDate sourceDate = Yourdate;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Australia/Melbourne"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; // your device time . you can also change any time zone like above.
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
答案 1 :(得分:1)
-(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";
}
答案 2 :(得分:0)
使用timeZoneForSecondsFromGMT
中的NSTimeZone
并为timezone
设置NSDateFormatter
。
NSString *dateString = @"2014-12-20 16:30:00 GMT+5:30";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// For GMT + 2.30, seconds will be 7200 (2 hours) + 1800 (30 minutes)
// For GMT + 5.30, seconds will be 18000 (5 hours) + 1800 (30 minutes)
// 60 seconds * 60 minutes * 5 hours + 60 seconds * 30 minutes
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
[formatter setTimeZone:timeZone];
NSDate *date =[formatter dateFromString:dateString];
NSString *newTimeZoneDateString = [formatter stringFromDate:date];
NSLog(@"%@",newTimeZoneDateString1);