时间戳

NSDate * date = [NSDate date];
    // 获取当前时间戳
    NSString * ctime = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    NSLog(@"时间戳为:%@",ctime);
    
    // 获取当前时间
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YY-MM-dd HH:mm"];
    NSString * time = [formatter stringFromDate:date];
    NSLog(@"%@",time);
    
    // 时间戳转化成时间字符串
    NSTimeInterval tempTime = [ctime longLongValue];
    // 时间戳转NSDate
    NSDate * ctimeDate = [NSDate dateWithTimeIntervalSince1970:tempTime];
    NSString * timeStr = [formatter stringFromDate:ctimeDate];
    NSLog(@"转化的时间:%@",timeStr);

  // 字符串时间转时间戳
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString * time = [NSString stringWithFormat:@"%ld", (long)[[formatter dateFromString:@"2016-07-22 04:00"] timeIntervalSince1970]];
    NSLog(@"%@",time);

记性不好的人,只能贴出来方便查看

你可能感兴趣的:(时间戳)