NSDate组织

时间:2015-06-01 04:12:12

标签: ios objective-c iphone twitter nsdate

我正在尝试以S,M,H格式显示时间戳,但是在Minutes格式的某些帐户上我得到负数。比所说的更好:

enter image description here

克里斯的推文应该是4米,而米娜的推文应该是6米。这是我的实现代码:

// 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 个答案:

答案 0 :(得分:0)

发生这种情况的一个主要原因是时区。

尝试将时区设置为GMT&然后你做计算。希望它有所帮助。