iOS常见时间戳的总结

//第一种方式

1普通格式转化,需要加减8个小时的时间

#if0

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

//设置日期格式,最后的结果以该格式为准

formatter.dateFormat =@"yy年MM月dd hh点mm分ss秒";

NSDate *n = [NSDate date];

NSString *result = [formatter stringFromDate:n];

NSLog(@"result = %@", n);

#endif


第二种格式转化,常用方法  //pub_time代表的是网络数据获取的参数属性

- (NSString*)pub_time    //pub_time 为网络请求数据的时间戳属性

{

NSString*ret =@"";

NSCalendar*cal = [NSCalendarcurrentCalendar];//日历对象

//将时间戳转换为日期对象

NSDate*pubtime = [NSDatedateWithTimeIntervalSince1970:_pub_time.doubleValue];

NSDate*now = [NSDatedate];

//获取指定日期的指定部分

NSDateComponents*pubComponents = [calcomponents:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinutefromDate:pubtime];

NSDateComponents*nowCompents = [calcomponents:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinutefromDate:now];

//针对不同需求进行选择

if(pubComponents.day- nowCompents.day!=0) {

ret = [NSStringstringWithFormat:@"%02ld-%02ld", pubComponents.month, pubComponents.day];

}

else

{

ret = [NSStringstringWithFormat:@"%02ld:%02ld", pubComponents.hour, pubComponents.minute];

}

returnret;

}

你可能感兴趣的:(iOS常见时间戳的总结)