How to turn a NSString into NSDate?

后端 未结 5 1312
攒了一身酷
攒了一身酷 2021-02-10 10:50

Ive been racking my brains with no luck. Could someone please tell me how i would convert this string:

\"2011-01-13T17:00:00+11:00\"

into a NSDate?

5条回答
  •  情歌与酒
    2021-02-10 11:23

    put the T part in single quotes, and check the unicode docs for the exact formatting. In my case, I have something similar, which I do this:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSS"];
    

    Again, not exactly the same, but you get the idea. Also, be careful of the timezones when converting back and forth between strings and nsdates.

    Again, in my case, I use:

    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
    

提交回复
热议问题