ios 时间类转化为字符串
// NSString* timeStr = [self stringWithDate: [NSDate dateWithTimeIntervalSince1970:userGifGrouptEntity.lastGiftSentTime / 1000]]
- (NSString *)stringWithDate:(NSDate *)date {
if (date == nil)
return @"";
NSDate *now = [NSDate date];
NSTimeInterval time = [now timeIntervalSinceDate:date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSHourCalendarUnit fromDate:now];
NSDateComponents *dateComponets = [calendar components:NSYearCalendarUnit fromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar: [NSCalendar autoupdatingCurrentCalendar]];
if ([nowComponents year] != [dateComponets year]) {
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
return [dateFormatter stringFromDate:date];
}
int nowHour = (int)[nowComponents hour];
int days_ago = ((int)time + 3600*(24-nowHour))/(3600*24);
if (days_ago == 0){
[dateFormatter setDateFormat:@"HH:mm"];
return [dateFormatter stringFromDate:date];
}else if(days_ago == 1) {
return @"Yesterday";
}else{
[dateFormatter setDateFormat:@"MM-dd"];
return [dateFormatter stringFromDate:date];
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。