简体   繁体   中英

NSDateFormatter strange behavior

I'm developing an iOs 4 application with latest SDK and XCode 4.2.

I'm using a JSON web service to get some data. One of those data is a date.

To convert that date parsed by SBJson, I use the following formatter:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];

I'm using an asynchronous connection, and when it gets all data from web service I use this code to create my own objects:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    NSString *json_string = [[NSString alloc] initWithData:receivedData
                                                  encoding:NSUTF8StringEncoding];
    NSDictionary* datos = [parser objectWithString:json_string error:nil]; 

    [connection release];
    [receivedData release];
    [parser release];
    [json_string release];

    NSArray* data = [datos objectForKey:kRootKey];

    cierres = [[NSMutableArray alloc] initWithCapacity:data.count];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];

    for (int i = 0; i < data.count; i++)
    {
        NSDictionary* object = [data objectAtIndex:i];

        NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]);
        NSString* fedName = [object objectForKey:kFederacionKey];
        NSDate* aperDate = [df dateFromString: [object objectForKey:kAperturaKey]];
        NSDate* cierreDate = [df dateFromString: [object objectForKey:kCierreKey]];

        CierreData* cierreData =
            [[CierreData alloc] initWithFederationName:fedName
                                              openDate:aperDate
                                          andCloseDate:cierreDate];
        [cierres addObject:cierreData];

        [cierreData release];
    }

    [delegate dataReceived];
}

This sentence NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]); shows the following output on console:

Parser: Dinamarca, Aper: 2012-01-05, Cierre: 2012-01-31

But, aperDate variable is 2012-01-04 23:00:00 +0000 .

Why am I getting 2012-01-04 when it must be 2012-01-05? I have also checked JSON data on my web browser and is 2012-01-05.

When you create a NSDate, automatically the time zone is set to GMT0, so you should set your time zone to the NSDate, for ex like this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"Europe/Madrid"];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:timezone];

hi may be this code will help you. try like this.

 NSDateFormatter *formater=[[NSDateFormatter alloc]init];
        [formater setDateFormat:@"yyyy-MM-dd h:mm:ss a"];
        [formater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

        NSDate *now1 = [formater dateFromString:[NSString stringWithFormat:@"%@ 11:07:00 PM",[[arrdate objectAtIndex:i]valueForKey:@"date"]]];

        NSLog(@"%@",now1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM