NSdate 与 NSString转换

  1. date类型转换为字符串,格式可以在这个格式字符串中自定义,例如加上时分秒

    -(NSString*)nsdateToString:(NSDate *)date
    {
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSString* string=[dateFormat stringFromDate:date];
    NSLog(@"%@",string);
    return string;
    }
    
  2. 字符串类型转换为NSDate

    - (NSDate *)dateFromString:(NSString *)dateString
     {
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat: @"yyyy-MM-dd"];
     NSDate *destDate= [dateFormatter dateFromString:dateString];
     return destDate;
     }  

你可能感兴趣的:(NSdate 与 NSString转换)