Ios NSTimeInterval显示错误的时间间隔
添加评论后,我需要在标签上显示评论时间,比如一年前、一天前、几分钟前和几秒钟前,但它返回的是-4425、-33434秒前的随机数 下面是我在应用程序中使用的代码Ios NSTimeInterval显示错误的时间间隔,ios,iphone,objective-c,xcode,Ios,Iphone,Objective C,Xcode,添加评论后,我需要在标签上显示评论时间,比如一年前、一天前、几分钟前和几秒钟前,但它返回的是-4425、-33434秒前的随机数 下面是我在应用程序中使用的代码 NSString *inDateStr = [NSString stringWithFormat:@"%@",[[[dict objectForKey:@"d"] objectAtIndex:indexPath.row-1] objectForKey:@"created"]]; NSString *s = @"yyyy-MM-dd HH
NSString *inDateStr = [NSString stringWithFormat:@"%@",[[[dict objectForKey:@"d"] objectAtIndex:indexPath.row-1] objectForKey:@"created"]];
NSString *s = @"yyyy-MM-dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-7.00"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone localTimeZone];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateStartingString = [[NSDate alloc] init];
NSString *datestartString = outDateStr;
dateStartingString = [dateFormatter dateFromString:datestartString];
NSTimeInterval timeDifference = [[NSDate date] timeIntervalSinceDate:dateStartingString];
double minutes = timeDifference / 60;
double hours = minutes / 60;
double seconds = timeDifference;
double days = minutes / 1440;
UILabel * dateLbl=[[UILabel alloc] initWithFrame:CGRectMake(155, [[CountArr objectAtIndex:indexPath.row] floatValue]-1, 80, 20)];
if(seconds>=86400)
dateLbl.text=[NSString stringWithFormat:@"%.0f days ", days];
else if(seconds>=3600 && seconds<86400 )
dateLbl.text=[NSString stringWithFormat:@"%.0f hours ", hours];
else if (seconds>=60 && seconds<3600 )
dateLbl.text=[NSString stringWithFormat:@"%.0f minutes ", minutes];
else
dateLbl.text=[NSString stringWithFormat:@"%.0f seconds ", seconds];
dateLbl.textColor=[UIColor lightGrayColor];
dateLbl.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:dateLbl];
我相信NSDateFormatter只接受一个字符串并返回一个NSDate对象。设置时区是为了设置NSDate的属性,但实际上并不转换提供的时间。所以最后如果你输入5:30GMT,你会得到5:30IST
您可能希望使用NSCalender进行时区转换。您可以使用此方法获取日期差异。 确保日期格式正确。如果您得到的是负值,只需检查日期格式
-(NSDateComponents *)getDateDifference:(NSString *)date
{
NSDate *dateA=[NSDate date]; //Current date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *dateB = [dateFormatter dateFromString:date];//Convert nsstring to nsdate
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; //Gregorian allocation
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:dateA
toDate:dateB
options:0]; //Get day and hour
return components;
}
// Call method like
NSDateComponents *difference = [self getDateDifference:marriageDate];
NSLog(@"diff year = %f",difference.year);
如果我也需要分钟和秒怎么办?您可以在组件中添加NSMinuteCalendarUnit | NSSecondCalendarUnit。日期格式只需添加HH:mm:ss即可。让我知道它是否有效。