【问题标题】:NSDate OrganizationNSDate 组织
【发布时间】:2015-08-14 10:36:56
【问题描述】:

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

Chris 的推文应该是 4m,Mina 的推文应该是 6m。这是我的实现代码:

// 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;
}

【问题讨论】:

  • minute == 1 > second 是做什么的?

标签: ios objective-c iphone twitter nsdate


【解决方案1】:

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

尝试将时区设置为格林威治标准时间,然后再进行计算。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多