GMT and UTC formatter with NSDateFormatter

让人想犯罪 __ 提交于 2019-12-22 10:32:14

问题


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.



来源:https://stackoverflow.com/questions/19024092/gmt-and-utc-formatter-with-nsdateformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!