For iPhone OS 4.0 “dateFromString” method of NSDateFormatter returns nil

前端 未结 3 1213
予麋鹿
予麋鹿 2021-02-06 06:12

I am using following code & its working perfectly fine in iPhone OS 3.2

+(NSDate *)NSDateFromString:(NSString *)dateString
{

 NSDateFormatter *dateFormatter         


        
相关标签:
3条回答
  • 2021-02-06 06:25

    - (id)init is not deprecated for NSDateFormatter in iOS4. That's an error in the documentation.

    0 讨论(0)
  • 2021-02-06 06:40

    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. It doesn't even follow the specification which states that it should return 0000 for the offset. So not only does it not return local date, it also uses GMT instead of an offset. It appears that this new format isn't compatible with NSDateFormatter methods.

    Also, I don't think it is an error in documentation. In the API Diff document, it lists that init is removed for NSDateFormatter.

    0 讨论(0)
  • 2021-02-06 06:41

    [NSDateFormatter dateFormatFromTemplate:options:locale:] might work better

    NSString *format = [NSDateFormatter
                       dateFormatFromTemplate:@"Mdjm"
                                      options:0
                                       locale:[NSLocale currentLocale]];
    

    // "M/d h:mm a" // US English format returned for US locale

    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:dateString];
    
    0 讨论(0)
提交回复
热议问题