NSDate format in iOS 4.1

前端 未结 3 448
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 11:22

[NSDate date]in iOS 4.0 used to return date in the format: 2010-09-15 09:28:26 +0530

but now in iOS 4.1 it returns the date in the format: 2010-09-15 09:28:26 GMT

相关标签:
3条回答
  • 2021-01-07 11:55

    In iOS2.x and 3.x the NSDate description/datefromstring function returns: 2010-09-24 17:30:00 - 0700

    in iOS 4.1 it returns this: 2010-09-25 00:30:00 GMT

    You could submit a bug to apple.

    0 讨论(0)
  • 2021-01-07 12:02

    Use NSDateFormatter

    0 讨论(0)
  • 2021-01-07 12:03

    Thank for your replies. I googled about the problem and somewhere I found that NSDate has a bug in iOS 4.1. So I solved the problem using following method

    - (NSString *)formattedStringUsingFormat:(NSString *)dateFormat
    {
        NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:dateFormat];
        [formatter setCalendar:cal];
        [formatter setLocale:[NSLocale currentLocale]];
        NSString *ret = [formatter stringFromDate:[NSDate date]];
        [formatter release];
        [cal release];
        return ret;
    }
    

    and I passed the format as

    [self formattedStringUsingFormat:@"yyyy-MM-dd HH:mm:ss ZZ"];
    
    0 讨论(0)
提交回复
热议问题