【问题标题】:How to show time which is less than a day?如何显示小于一天的时间?
【发布时间】:2016-07-01 07:28:16
【问题描述】:

此代码将返回两天之间的天数。

如何确定时间何时过去,一天变为小时(20 小时)或(40 分钟),那么我如何检查它是一天还是不到一天。

-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate
{

    NSCalendar *calendar = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *components = [calendar components:NSCalendarUnitDay
                                                fromDate:self
                                                  toDate:aDate options:kNilOptions];

    return [components day];    
}

【问题讨论】:

  • 你用过 NSCalendarUnitDay,试试 unitHour 或 UnitMinute?
  • 使用timeIntervalSinceDate: 得到剩余时间是秒,结果 / 3600 剩余小时数, (result % 3600) / 60 剩余分钟数和 (result %3600) % 60 剩余秒数。

标签: ios objective-c nsdate nscalendar


【解决方案1】:

请改用NSCalendarUnitSecond

NSDateComponents *components = [calendar components:NSCalendarUnitSecond
                                               fromDate:date
                                                 toDate:aDate options:kNilOptions]; 

稍后您可以将秒转换为任何单位(小时、天、年)。

【讨论】:

  • 但是如何确定时间什么时候过去,一天变成几个小时(20小时),那么我如何检查它是一天还是一天以下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 2011-08-30
  • 2010-12-14
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
相关资源
最近更新 更多