NSDate format in iOS 4.1

岁酱吖の 提交于 2019-11-30 20:53:24

问题


[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

In my application it is leading to lots of problems.

Does anyone know how to fix this?

Thanks in advance, YPK


回答1:


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



回答2:


Use NSDateFormatter




回答3:


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.



来源:https://stackoverflow.com/questions/3716384/nsdate-format-in-ios-4-1

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