NSData小结

作者: 郑州程序员王一 | 来源:发表于2016-09-06 23:44 被阅读14次

    获取字典里的时间戳 转换为时间

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[_dict[@"ctime"] doubleValue]/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    NSString *stringTime = [formatter stringFromDate:date];
    self.timeLabel.text = stringTime;
    

    获取当前时间的方法

    -(NSString *)timeDate{
         NSDate *date = [NSDate date];
         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
         formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
         NSString *string = [formatter stringFromDate:date];
         return string;
    }
    

    获取当前时间戳

    -(NSString *)timeDate{
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval timestamp = [dat timeIntervalSince1970];
        NSString *timeString = [NSString stringWithFormat:@"%.0f", timestamp];
        return timeString;
    }
    

    获得当前:年、月、日

        //这个是NSDate类型的日期,所要获取的年月日都放在这里;
        NSDate *date = [NSDate date];
        NSCalendar *cal = [NSCalendar currentCalendar];
        //这句是说你要获取日期的元素有哪些。获取年就要写NSYearCalendarUnit,获取小时就要写NSHourCalendarUnit,中间用|隔开;
        unsigned int unitFlags = NSCalendarUnitMonth;
        //把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面;
        NSDateComponents *d = [cal components:unitFlags fromDate:date];
        //然后就可以从d中获取具体的年月日了;
        NSInteger month = [d month];
    

    勤学如早春之苗,不见其增,日有所涨。
    辍学如磨刀之石,不见其减,日有所损。

    相关文章

      网友评论

        本文标题:NSData小结

        本文链接:https://www.haomeiwen.com/subject/lvqjettx.html