• iOS 时间戳的问题


    - (NSString *)timeWithTimeIntervalString:(NSString *)timeString
    {
        // 格式化时间
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
        
        // 毫秒值转化为秒
        NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
        NSString* dateString = [formatter stringFromDate:date];
        return dateString;
    }
    时间转化为时间戳
       
         // 当前时间
         NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
        NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
        通过比较时间与当前时间返回年月日的方法
    - (void)getBabyDetailAge:(NSString *)date
    {
        // 获得日期对象
        NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
        formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSDate *createDate = [formatter_ dateFromString:date];
        
        NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
        NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
        NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];
        
        NSInteger years = [components year];
        NSInteger months = [components month ];
        NSInteger days = [components day ];
    }
  • 相关阅读:
    CF149D Coloring Brackets
    CF508D
    CF483C Diverse Permutation
    【纪念】我写过几乎最长的代码
    .net core图片上传详解
    layui插件croppers的使用
    关于日常操作中sql的性能
    leeCode 278
    leeCode刷题 1078
    leeCode刷题 lc184
  • 原文地址:https://www.cnblogs.com/dlwj/p/5763023.html
Copyright © 2020-2023  润新知