• 字符串转换成NSDate类型的 为nil解决方法


    方法一    通过下列函数来解决   但是得到的日期会改变 修改方法fix
     
    - (NSDate *)timeForString:(NSString *)string {
       
        NSMutableString *timeString = [[NSMutableString alloc] initWithString:string];
       
        [timeString setString:[timeString stringByReplacingOccurrencesOfString:@"/Date(" withString:@""]];
       
        [timeString setString:[timeString stringByReplacingOccurrencesOfString:@")/" withString:@""]];
       
        [timeString setString:[timeString substringToIndex:timeString.length - 3]];
       
        return [NSDate dateWithTimeIntervalSince1970:[timeString longLongValue]];
       
    }
     
     
    方法二  已解决
        NSString *time = [_userDic objectForKey:USERAGE];
                            NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
                            [formatter1 setDateFormat:@"yyyyMMdd"];
                           
                            [formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GTM"]];//GTM,格林尼治时区
                            NSDate *date = [formatter1 dateFromString:time];
                           
                            NSTimeInterval dateDiff = [date timeIntervalSinceNow];
                            int age= fabs(trunc(dateDiff / (60 * 60 * 24)) / 365);
                            cell.value.text = [NSString stringWithFormat:@"%d", age];
  • 相关阅读:
    DOM节点的创建
    js中css样式
    js中面向对象
    js
    this是什么!
    事件
    Dom
    逻辑运算和作用域的问题
    Json
    数组
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4849762.html
Copyright © 2020-2023  润新知