search

Home  >  Q&A  >  body text

objective-c - NSString 转 NSDate

给我你的怀抱给我你的怀抱2763 days ago499

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-04-27 09:04:47

    This should be the conversion between NSDate and NSString. You can use this method to convert after getting the date, instead of using this method to get the date

    reply
    0
  • 阿神

    阿神2017-04-27 09:04:47

    1. First, the NSString passed in must be in a fixed format.
    2. Then set NSDateFormatter to follow the same format.
    3. Convert to time. If it is still nil... (As if I didn't say it----)

     NSString *str =  @"2015-11-26 17:51:51"; 
     NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
     [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSDate *date = [dateformatter dateFromString: str];

    reply
    0
  • Cancelreply