Ios NSDate组织
我试图以S,m,H格式显示时间戳,但是在一些分钟格式的帐户上我得到负数。表现得比说的好: 克里斯的推文应该是400万条,米娜的推文应该是600万条。以下是我的实现代码:Ios NSDate组织,ios,objective-c,iphone,twitter,nsdate,Ios,Objective C,Iphone,Twitter,Nsdate,我试图以S,m,H格式显示时间戳,但是在一些分钟格式的帐户上我得到负数。表现得比说的好: 克里斯的推文应该是400万条,米娜的推文应该是600万条。以下是我的实现代码: // Set Timestamp NSDate *date = [NSDate dateWithTimeIntervalSince1970:[entry[@"created_time"] doubleValue]]; NSDate *currentDateNTime = [NSDate date]; NSCalendar *c
// Set Timestamp
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[entry[@"created_time"] doubleValue]];
NSDate *currentDateNTime = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *instacomponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSInteger instahour = [instacomponents hour];
NSInteger instaminute = [instacomponents minute];
NSInteger instasecond = [instacomponents second];
NSDateComponents *realcomponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:currentDateNTime];
NSInteger realhour = [realcomponents hour];
NSInteger realminute = [realcomponents minute];
NSInteger realsecond = [realcomponents second];
NSInteger hour = realhour - instahour;
NSInteger minute = realminute - instaminute;
NSInteger second = realsecond - instasecond;
int adjustedSeconds = ((int)minute * 60) - abs((int)second);
int adjustedMinutes = adjustedSeconds / 60;
if (hour==1 > minute > 0) {
int negmin = ((int)hour * 60) - abs((int)minute);
int posmin = abs(negmin);
NSString *strInt = [NSString stringWithFormat:@"%dm",posmin];
cell.timeAgo.text = strInt;
}else if (hour>0){
NSString *strInt = [NSString stringWithFormat:@"%ldh",(long)hour];
cell.timeAgo.text = strInt;
}else if (hour==1){
NSString *strInt = [NSString stringWithFormat:@"%ldh",(long)hour];
cell.timeAgo.text = strInt;
}else if(minute == 1 > second){
NSString *strInt = [NSString stringWithFormat:@"%lds",(long)second];
cell.timeAgo.text = strInt;
}else{
NSString *strFromInt = [NSString stringWithFormat:@"%dm",adjustedMinutes];
cell.timeAgo.text = strFromInt;
}
发生这种情况的一个主要原因是时区
尝试将时区设置为格林尼治标准时间,然后再进行计算。希望有帮助。分钟==1>秒做什么?