NSString处理NSDate里面的零
注:这里的NSString 是只读的,所以我们这里采用NSMutableString 转换并生成新的字符串
//获取当前时间
NSDate *currentDate = [NSDate date];
// NSLog(@"%@",currentDate); //2016-03-17 07:30:05 +0000这里显示标准时间,非本地时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *dateNow =[dateFormatter stringFromDate:currentDate]; //用以上格式显示当前时间
NSLog(@"%@",dateNow);
//下面需要删除第六个字符如果为 0 用数组遍历字符串,取出第六个字符
NSString *deleteZero = [dateNowsubstringWithRange:NSMakeRange(5, 1)];
NSLog(@"取到的第六个字符%@",deleteZero);
if ([deleteZero isEqualToString:@"0"]) {
NSLog(@"字符串等于0");
NSMutableString *muStr = [NSMutableString stringWithString:dateNow];
NSLog(@"删除前的%@",muStr);
[muStr deleteCharactersInRange:NSMakeRange(5, 1)];
NSLog(@"删除后的%@",muStr);
dateNow = [NSString stringWithFormat:@"%@",muStr];
NSLog(@"%@",dateNow);
}
NSLog(@"%@",dateNow);