使用NSDateFormatter的GMT和UTC格式化程序

时间:2013-09-26 09:16:46

标签: ios nsdateformatter

我编写了转换当前时间utc-gmt或gmt -utc的函数。如果msgArrivedDate为null,则函数正常工作。如果不是(这意味着,msgArrivedDate来自不能转换的休息服务。

jSON解析部分:

NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0];

    for(int i=0 ;i< [messageSentTime count]; i++)
    {
        //[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]];

        NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]);

功能部分:

-(id)timeZoneFormatter:(NSString *)formatType : (NSString *)msgArrivedDate
{
    NSDate *date;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


    if([msgArrivedDate length] > 0)
    {

        date = [dateFormatter dateFromString:msgArrivedDate];

    } else {

        date = [NSDate date];
    }


    if([formatType isEqualToString:@"UTC"])
    {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

    }

    if([formatType isEqualToString:@"GMT"])
    {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

    }

    NSString *dateString = [dateFormatter stringFromDate:date];

    return dateString;
}

ReST将我转换为UTC格式的这些值:

"2013-09-24 15:03:17",
"2013-09-25 12:09:22",
"2013-09-25 13:07:45",
"2013-09-25 13:08:19",
"2013-09-25 14:22:38"

当我调用该函数时,(NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]))返回:

 messageSentTime = (
    "2013-09-24 15:03:17",
    "2013-09-25 12:09:22",
    "2013-09-25 13:07:45",
    "2013-09-25 13:08:19",
    "2013-09-25 14:22:38" )

我想我只是错过了这里的小问题:(找不到......

1 个答案:

答案 0 :(得分:14)

改变这个:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

到此:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

编辑:此方法将处理将日期字符串从UTC转换为GMT以及从GMT转换为UTC:

- (id)translateDate:(NSString *)msgArrivedDate
               from:(NSString *)fromTimeZone
                 to:(NSString *)toTimeZone {
    NSDate *date = nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    if([fromTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([fromTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    if([msgArrivedDate length] > 0) {
        date = [dateFormatter dateFromString:msgArrivedDate];
    } else {
        date = [NSDate date];
    }

    if([toTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([toTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

将“2013-09-25 14:22:38”从UTC转换为GMT(即当地时间,使用GMT)将导致2013-09-25 15:22:38,同样,转换“2013-09-25 14:22:38“从GMT到UTC将产生2013-09-25 13:22:38