GMT and UTC formatter with NSDateFormatter

2019-05-16 13:59发布

问题:

i wrote function that converting current time utc-gmt or gmt -utc. The functions just works fine if msgArrivedDate is null. If it's not ( that means , msgArrivedDate comes from rest service that doses not convert .

jSON parse part :

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]]);

Function part :

-(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 returned me to these values in UTC format :

"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"

When i call the function, (NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]])) returns :

 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" )

I think i am just missing little issue over here :( couldn't find yet ...

回答1:

Change this:

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

to this:

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

Edit: This method will handle translating the date string from UTC to GMT and from GMT to 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;
}

Converting "2013-09-25 14:22:38" from UTC to GMT (i.e. local time, in your usage of GMT) will result in 2013-09-25 15:22:38, and similarly, converting "2013-09-25 14:22:38" from GMT to UTC will result in 2013-09-25 13:22:38.