【发布时间】:2015-08-14 13:13:31
【问题描述】:
最初的目标是使用 NSDateComponents 计算两个 NSDate 之间的差异。此外,一旦获得并验证正确的天、小时、分钟和秒。在 UITableViewCell 中显示具有差异(到最接近的分钟、小时或天)的实时代码(计数器)。 目前的进展可以在下面找到。 任何建议或完成此任务的方向将不胜感激!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [(AGSGraphic *)[_radios objectAtIndex:indexPath.row] attributeForKey:@"parent_id"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
NSNumber *timestamp = [[_radios objectAtIndex:indexPath.row] valueForKey:@"date_time_edt"];
NSDate *today = [NSDate date];
NSDate *crewDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]/1000];
NSString *temp = [df stringFromDate:today];
NSString *crewtemp = [df stringFromDate:crewDate];
crewDate = [df dateFromString:crewtemp];
today = [df dateFromString:temp];
NSLog(@"%@", crewDate);
NSLog(@"%@", today);
NSTimeInterval interval = [crewDate timeIntervalSinceDate:today];
interval = -interval;
NSInteger minutes = interval/60;
NSInteger hours = minutes/60;
NSInteger days = hours/24;
// NSInteger weeks = days/7;
// NSInteger months = weeks/4;
// NSInteger years = months/12;
// NSTimeInterval years = interval/31536000;
// NSTimeInterval months = years/12;
// NSTimeInterval weeks = years/52;
// NSTimeInterval days = years/365;
// NSTimeInterval hours = days/24;
// NSTimeInterval minutes = hours/60;
// NSLog(@"%f", interval);
if(minutes < 60)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld minuntes ago", (long)minutes];
}
else if(hours < 24)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld hours ago", (long)hours];
}
else
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld days ago", (long)days];
}
return cell;
}
【问题讨论】:
-
NSDateIntervalFormatter
-
你能举一两个例子来说明你希望最终字符串的样子吗?
-
YYYY-MM-dd hh:mm:ss,从这里我会将组件传递到一个方法中,该方法将在屏幕上打印差异(四舍五入到最接近的分钟、小时或天)
-
四舍五入到最接近的小时、分钟或天,但您仍想使用包含秒的格式字符串(
ss)?
标签: ios nsdate nsdateformatter nscalendar nsdatecomponents