时间戳转时间 时间转时间戳

//现在时刻的时间戳

NSDate *newDate =[NSDate date];
NSString *timeStamp =[NSString stringWithFormat:@"%lu", (long)[newDate timeIntervalSince1970]];


//时间转换为时间戳的转换

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];//时间的格式要正确
NSDate *weiBoDate = [dateFormatter dateFromString:@"2018-07-08 08:51:20 +0000"];//与上面的时间格式对应
NSString *weiBoDateStamp = [NSString stringWithFormat:@"%lu", (long) [weiBoDate timeIntervalSince1970]];//获取时间戳

//时间戳转换为时间
NSDate *timeStamp =[NSDate dateWithTimeIntervalSince1970:1296035591];//时间戳的起始点设置
NSLog(@"1296035591 = %@",timeStamp);
NSString *timeStampStr = [dateFormatter stringFromDate:timeStamp];

NSLog(@"confromTimespStr=  %@",timeStampStr);

if([timeStamp compare:@"157679999"]){//比较时间戳是否过期
    NSLog(@"时间还没有过期");
}else{
    NSLog(@"时间已经过期");
}

comments powered by Disqus