如何在ios7中将GMT转换为EST

时间:2014-05-08 06:54:19

标签: ios objective-c ios7

日期&时间格式为“yyyyMMddHHmmss”。我无法将此时间转换为EST时区。我遵循一些教程,但我没有得到正确的结果。如果有任何API进行此转换,请告诉我。

3 个答案:

答案 0 :(得分:2)

您应该按照以下方式进行:

1)首先,创建一个NSDateFormatter来获取NSDate

NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
[serverFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[serverFormatter setDateFormat:@"'at 'HH:mm' of 'dd/MM/yyyy"];

2)将字符串(将其定义为theString)转换为NSDate:

NSDate *theDate = [serverFormatter dateFromString:theString];

3)创建一个NSDateFormatter以将theDate转换为用户:

NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:@"HH:mm dd/MM/yyyy"];
[userFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];

3)从userFormatter获取字符串:

 NSString *dateConverted = [userFormatter stringFromDate:theDate];

希望这可以帮助你!!!

答案 1 :(得分:1)

    NSString *startTime = @"20140508063630";
    NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init]; 
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"EST"];
    [workingHoursFormatter setTimeZone:timeZone];
    [workingHoursFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate *openingTime = [workingHoursFormatter dateFromString:startTime];

试试吧。在这里工作。

答案 2 :(得分:1)

NSDateFormatter *inputDateFormat = [[NSDateFormatter alloc] init];
[inputDateFormat setDateFormat:@"yyyyMMddHHmmss"];
[inputDateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSDateFormatter *outputDateFormat = [[NSDateFormatter alloc] init];
[outputDateFormat setDateFormat:@"yyyyMMddHHmmss"];
[inputDateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];

NSString *outputDate = [outputDateFormat stringFromDate:[inputDateFormat dateFromString:@"20140508063630"]];
NSLog(@"%@",outputDate);