【问题标题】:Convert directly from NSTimeInterval to NSDateComponents to NSString?直接从 NSTimeInterval 转换为 NSDateComponents 到 NSString?
【发布时间】:2014-06-13 04:46:05
【问题描述】:

我有几个NSTimeIntervals 来自之前对各种NSDates 的计算。现在我想以days:hours:minutes:seconds 格式向用户显示这些间隔。

在我的应用程序的早期,我使用此代码在稍微不同的上下文中显示信息:

-(void)updateDurationLabel
{
    //Which calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Gets the componentized interval from the most recent time an activity was tapped until now

    NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:self.startTime toDate:[NSDate date] options:0];
    NSInteger days = [components day];
    NSInteger hours = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds =[components second];

    // Converts the components to a string and displays it in the duration label, updated via the timer

    cellLabelTempText = [NSString stringWithFormat:@"%02i:%02i:%02i:%02i",days,hours,minutes,seconds];
    self.durationOrSleepLabel.text = cellLabelTempText;
}

但是,在当前情况下,我希望使用各种已获得的NSTimeIntervals 生成所需的NSString,以便在动态创建的多个不同标签中使用。

问题:

有没有办法从现有的NSTimeInterval 直接访问NSString,即不通过fromDate/toDate rigamarole?

谢谢!

更新:

根据@Rich 在下面的回答,我添加了这种方法,它绕过了数学:

-(void) focusItemDurationCalculator
{
    NSInteger days = ((NSInteger) focusItemDuration) / (60 * 60 * 24);
    NSInteger hours = (((NSInteger) focusItemDuration) / (60 * 60)) - (days * 24);
    NSInteger minutes = (((NSInteger) focusItemDuration) / 60) - (days * 24 * 60) - (hours * 60);
    NSInteger seconds = ((NSInteger) round(focusItemDuration)) % 60;

    actualDurationFocusItem = [NSString stringWithFormat:@"%02i:%02i:%02i:%02i",days,hours,minutes,seconds];
}

效果很好!

【问题讨论】:

    标签: ios nsstring nsdatecomponents nstimeinterval


    【解决方案1】:

    为什么不直接使用NSTimeInterval 并自己计算每个组件。您也可以不使用[NSDate date],而使用timeIntervalSinceNow
    您可以使用整数除法来丢弃每个组件的小数部分。

    // Use abs if you don't care about direction
    NSTimeInterval duration = abs([self.startTime timeIntervalSinceNow]);
    
    NSInteger days = ((NSInteger) duration) / (60 * 60 * 24);
    NSInteger hours = (((NSInteger) duration) / (60 * 60)) - (days * 24);
    NSInteger minutes = (((NSInteger) duration) / 60) - (days * 24 * 60) - (hours * 60);
    NSInteger seconds = ((NSInteger) round(duration)) % 60;
    

    这应该比处理NSDateComponents 更快。这不完全是您正在寻找的东西,但它确实涵盖了这一点:D

    无需通过 fromDate/toDate rigamarole

    【讨论】:

    • @TimJones 我很有钱:P
    • 虽然如果你在问题更新中输入正确的名字我会放过你的,哈哈:D
    • 对不起,丰富!系统把回复的顺序颠倒了,我给更新打分的时候连文字都没看。现在已修复,再次感谢!
    【解决方案2】:

    很遗憾,无法跳过计算步骤。我建议您在 NSDateNSString 上写一个类别,并在那里实现所需的转换。

    【讨论】:

    • 谢谢,尼古拉斯!原来是有办法的。快速而肮脏,但完全足以解决手头的问题。请参阅上面的更新。
    • 嗨,蒂姆,好吧,但是这个也取代了 NSDate(这就是在 [self.startTime timeIntervalSinceNow] 中发生的事情),所以这不是直接来自 NSTimeInterval。无论如何,很高兴它现在对你有用!顺便说一句,如果您更频繁地使用此代码,我建议您将其放在一个类别中,以便您可以在需要时轻松访问它:-)
    • @Nikolas-- 说得好。但是,在这种特殊情况下,正如我在问题中指出的那样,NSTimeIntervals 已经在其他地方计算过,因此已经可以作为双精度数使用。
    猜你喜欢
    • 2012-02-29
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多