获取以 UILabel (objective-c) 形式经过的时间



我想在我的单元格中添加一个自定义UILabel,显示项目发布的时间。有什么好的链接可以简单吗?

我想在情节提要中向我的单元格添加一个标签,为其分配一个标签,比如 22,然后以编程方式计算自用户发布项目以来的时间。

编辑:我不在乎我的nil值,所以我将我的代码更改为下面。 现在有人可以指出我添加时间戳的正确方向或扔给我一块骨头吗? 非常感谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
PFObject *group = [self.groups objectAtIndex:indexPath.row];
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [group objectForKey:@"name"];
UILabel *usernameLabel = (UILabel*) [cell viewWithTag:103];
usernameLabel.text = [group objectForKey:@"creatorName"];

return cell;
}
postedDate = // NSDate object of your postedDate
[postedDate timeIntervalSinceDate:[NSDate date];

这将返回日期之间的秒数,然后只需使用一些数学来获得分钟

NSString *strTime=[aryNewsFeedData valueForKey:@"created"][section];
 int days,hours,minutes;
  NSDateFormatter *formatter    = [[NSDateFormatter alloc] init];
  [formatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSS'Z'"];
  NSDate *chatTime   = [formatter dateFromString:strTime];
  NSDate *now =[formatter dateFromString:resultString];
  NSTimeInterval secondsBetween = [now timeIntervalSinceDate:chatTime];
  days            = secondsBetween / (60 * 60 * 24);
  secondsBetween -= days * (60 * 60 * 24);
  hours           = secondsBetween / (60 * 60);
  secondsBetween -= hours * (60 * 60);
  minutes         = secondsBetween / 60;

如果你的发布日期是NSDate,请查看NSCalendar -components:fromDate:toDate:options以获取经过的时间。这将为您提供各个组件(小时,分钟,秒...无论你要求什么)

NSDateFormatter *df = [NSDateFormatter new];
    NSString *SomeDate = @"22 04 2015";
    [df setDateFormat:@"dd MM yyyy"];       //Remove the time part
    NSString *TodayString = [df stringFromDate:[NSDate date]];
    NSDate *sdate = [df dateFromString:stringDate];
    NSString *TargetDateString = [df stringFromDate:sdate];
    NSTimeInterval time = [[df dateFromString:TargetDateString] timeIntervalSinceDate:[df dateFromString:TodayString]];
    int days = time / 60 / 60/ 24;
    _lableText.text = [NSString stringWithFormat:@"%d",days];

日期和时间示例和 如何在iOS
中计算两个日期之间的时差(以分钟为单位)浏览这些链接。

这可能有助于您:)

查看 FormatterKit。我相信这就是你要找的。

除此之外,当我不知道这个库的存在时,我做了这个简单的类方法,它返回相对于当前时间的格式化时间戳(适用于 unix 时间戳)。只需将 unix 时间戳作为字符串传递给它,它将返回一个字符串,其中包含相对于当前时间的相关时间戳:

+ (NSString*) getTimestampForDate:(NSString*)dateString {
    NSDate* sourceDate = [NSDate dateWithTimeIntervalSince1970:[dateString doubleValue]];
    NSDate* currentDate = [NSDate date];
    NSCalendar* currentCalendar = [NSCalendar currentCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *differenceComponents = [currentCalendar components:unitFlags fromDate:sourceDate toDate:currentDate options:0];
    NSInteger yearDifference = [differenceComponents year];
    NSInteger monthDifference = [differenceComponents month];
    NSInteger dayDifference = [differenceComponents day];
    NSInteger hourDifference = [differenceComponents hour];
    NSInteger minuteDifference = [differenceComponents minute];
    NSString* timestamp;
    if (yearDifference == 0
        && monthDifference == 0
        && dayDifference == 0
        && hourDifference == 0
        && minuteDifference <= 2) {
        //"Just Now"
        timestamp = @"Just Now";
    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 0
               && minuteDifference < 60) {
        //"13 minutes ago"
        timestamp = [NSString stringWithFormat:@"%ld minutes ago", (long)minuteDifference];
    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 1) {
        //"1 hour ago" EXACT
        timestamp = @"1 hour ago";
    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference < 24) {
        timestamp = [NSString stringWithFormat:@"%ld hours ago", (long)hourDifference];
    } else {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSString* strDate, *strDate2 = @"";
        if (yearDifference == 0
            && monthDifference == 0
            && dayDifference == 1) {
            //"Yesterday at 10:23 am", "Yesterday at 5:08 pm"
            [formatter setDateFormat:@"hh:mm a"];
            strDate = [formatter stringFromDate:sourceDate];
            timestamp = [NSString stringWithFormat:@"Yesterday at %@", strDate];
        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference < 7) {
            //"Tuesday at 7:13 pm"
            [formatter setDateFormat:@"EEEE"];
            strDate = [formatter stringFromDate:sourceDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:sourceDate];
            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
        } else if (yearDifference == 0) {
            //"July 4 at 7:36 am"
            [formatter setDateFormat:@"MMMM d"];
            strDate = [formatter stringFromDate:sourceDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:sourceDate];
            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
        } else {
            //"March 24 2010 at 4:50 am"
            [formatter setDateFormat:@"d MMMM yyyy"];
            strDate = [formatter stringFromDate:sourceDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:sourceDate];
            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
        }
        [formatter release];
    }
    return timestamp;
}

我使用此方法并为其提供我的项目的 NSDate 或(我的日期的 NSString)作为参数。(就我而言,是一条消息)。如果您想传递 NSDate,只需使用日期格式化程序删除第一位。(我个人分别使用这两种方法)

+ (NSString *)dateDiff:(NSString *)origDate{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];   // 
    [df setFormatterBehavior:NSDateFormatterBehavior10_4];  // remove this is you want to pass an NSDate as parameter.
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];          // 
    NSDate *convertedDate = [df dateFromString:origDate];   // 
    NSDate *todayDate = [NSDate date];
    double ti = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1) {
        return NSLocalizedString(@"REL_TIME_NOW", nil);
    } else  if (ti < 60) {
        return NSLocalizedString(@"REL_TIME_LESS_THAN_MINUTE", nil);
    } else if (ti < 3600) {
        int diff = round(ti / 60);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTE", nil), diff];
        }else{
          return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTES", nil), diff];
        }
    } else if (ti < 86400) {
        int diff = round(ti / 60 / 60);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOUR", nil), diff];
        }else{
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOURS", nil), diff];
        }
    } else {
        int diff = round(ti / 60 / 60 / 24);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAY", nil), diff];
        }else{
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAYS", nil), diff];
        }
    }
}

这里是本地化字符串:

"REL_TIME_NOW"                  = "Right now!";
"REL_TIME_LESS_THAN_MINUTE"     = "Less than a minute ago";
"REL_TIME_MINUTE"               = "%d minute ago";
"REL_TIME_MINUTES"              = "%d minutes ago";
"REL_TIME_HOUR"                 = "%d hour ago";
"REL_TIME_HOURS"                = "%d hours ago";
"REL_TIME_DAY"                  = "%d day ago";
"REL_TIME_DAYS"                 = "%d days ago";

我只是没有勇气在上面的代码中替换它们。 :D

例如,这将返回一个字符串,上面写着"3 分钟前",其工作方式类似于一个超级按钮。

最新更新