【问题标题】:NSTimeInterval to NSDateNSTimeInterval 到 NSDate
【发布时间】:2011-12-19 18:50:24
【问题描述】:

如何将NSTimeInterval 转换为NSDate?把它想象成一个秒表。我希望初始日期为 00:00:00,并且我有 X 秒的 NSTimeInterval

我需要这样做,因为 NSTimeInterval 需要通过使用 lround 进行四舍五入转换为 int,然后转换为 NSDate 以使用 NSDateFormatter 将其放入字符串.

【问题讨论】:

    标签: objective-c nsdate nsdateformatter nstimeinterval


    【解决方案1】:

    NSTimeIntervalNSDate 在 Swift 中的转换:

    let timeInterval = NSDate.timeIntervalSinceReferenceDate() // this is the time interval
    NSDate(timeIntervalSinceReferenceDate: timeInterval)
    

    【讨论】:

      【解决方案2】:

      正如 Josh 详细回答的正确方法,如果您仍然希望间隔为 NSDate 格式,您可以使用以下方法

      + (NSDate *) dateForHour:(int) hour andMinute: (int) minute{
           NSDateComponents * components = [NSDateComponents new];
           components.hour = hour;
          components.minute = minute;
          NSDate * retDate = [[NSCalendar currentCalendar] dateFromComponents:components];
      return retDate;}
      

      【讨论】:

        【解决方案3】:

        通过apple developers:

        //1408709486 - 时间间隔值

        NSDate *lastUpdate = [[NSDate alloc] initWithTimeIntervalSince1970:1408709486];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        
        NSLog(@"date time: %@", [dateFormatter stringFromDate:lastUpdate]);
        

        您将获得: 日期时间:2014 年 8 月 22 日下午 3:11:26

        【讨论】:

          【解决方案4】:

          这里显示了一个简单的往返转换:

           NSDate * now = [NSDate date];
           NSTimeInterval  tiNow = [now timeIntervalSinceReferenceDate]; 
           NSDate * newNow = [NSDate dateWithTimeIntervalSinceReferenceDate:tiNow];
          

          奥莱·霍恩斯

          【讨论】:

            【解决方案5】:

            如果您希望显示时间间隔,我建议您不要使用NSDateFormatter。当您希望在本地或特定时区显示时间时,NSDateFormatter 很有用。但在这种情况下,如果时间经过时区调整(例如,一年中的一天有 23 小时),这将是一个错误。

            NSTimeInterval time = ...;
            NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li",
                                                          lround(floor(time / 3600.)) % 100,
                                                          lround(floor(time / 60.)) % 60,
                                                          lround(floor(time)) % 60];
            

            【讨论】:

            • 完美!正是我需要的!
            【解决方案6】:

            NSTimeInterval,正如它的名字,嗯,暗示,并不代表与NSDate 相同的东西。 NSDate 是一个时刻。时间间隔是一段时间。要从一个区间得到一个点,你必须有另一个点。您的问题就像在问“如何将 12 英寸转换为我正在切割的板上的一个点?”嗯,12 英寸,从哪里开始

            您需要选择一个参考日期。这很可能是NSDate,代表您启动计数器的时间。然后你可以使用+[NSDate dateWithTimeInterval:sinceDate:]-[NSDate dateByAddingTimeInterval:]

            也就是说,我很确定你是在倒过来考虑这个问题。您试图显示自某个起点以来经过的时间,即间隔,而不是当前时间。每次更新显示时,您应该只使用新的间隔。例如(假设您有一个定期触发的计时器来进行更新):

            - (void) updateElapsedTimeDisplay: (NSTimer *)tim {
            
                // You could also have stored the start time using
                // CFAbsoluteTimeGetCurrent()
                NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow];
            
                // Divide the interval by 3600 and keep the quotient and remainder
                div_t h = div(elapsedTime, 3600);
                int hours = h.quot;
                // Divide the remainder by 60; the quotient is minutes, the remainder
                // is seconds.
                div_t m = div(h.rem, 60);
                int minutes = m.quot;
                int seconds = m.rem;
            
                // If you want to get the individual digits of the units, use div again
                // with a divisor of 10.
            
                NSLog(@"%d:%d:%d", hours, minutes, seconds);
             }
            

            【讨论】:

              【解决方案7】:

              如果您将初始日期存储在 NSDate 对象中,您可以在未来的任何时间间隔内获取新日期。只需像这样使用dateByAddingTimeInterval:

              NSDate * originalDate = [NSDate date];
              NSTimeInterval interval = 1;
              NSDate * futureDate = [originalDate dateByAddingTimeInterval:interval];
              

              【讨论】:

                猜你喜欢
                • 2013-08-05
                • 1970-01-01
                • 2012-12-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-02-25
                • 2014-10-04
                • 1970-01-01
                相关资源
                最近更新 更多