【问题标题】:Objective-c format time left until certain dateObjective-c 格式化时间到某个日期为止
【发布时间】:2011-10-26 21:59:24
【问题描述】:

我正在创建一个倒数计时器,我需要打印出到特定日期的剩余时间(小时:分钟:秒)。我找到了如何获取现在和目标日期之间的时间间隔,但我不知道如何将时间间隔格式化为字符串。 NSDateFormater 是否适用于 NSTimeInterval?

【问题讨论】:

    标签: objective-c nsdate nstimeinterval


    【解决方案1】:

    您将首先比较两个 NSDate 对象以检索两者之间的秒数差异,您应该使用的 NSDate 方法是

    - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
    

    然后你可以简单地编写一个函数来将秒解析为小时/分钟/秒,例如你可以使用这个(未经测试):

    -(NSDictionary*)createTimemapForSeconds:(int)seconds{
       int hours = floor(seconds /  (60 * 60) );
    
       float minute_divisor = seconds % (60 * 60);
       int minutes = floor(minute_divisor / 60);
    
       float seconds_divisor = seconds % 60;
       seconds = ceil(seconds_divisor);
    
       NSDictionary * timeMap = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInt:hours], [NSNumber numberWithInt:minutes], [NSNumber numberWithInt:seconds], nil] forKeys:[NSArray arrayWithObjects:@"h", @"m", @"s", nil]];
    
       return timeMap;
    }
    

    【讨论】:

      【解决方案2】:

      NSTimeInterval 以秒为单位,使用除法和余数将其分解并格式化(代码未经测试):

      NSString *timeIntervalToString(NSTimeInterval interval)
      {
         long work = (long)interval; // convert to long, NSTimeInterval is *some* numeric type
      
         long seconds = work % 60;   // remainder is seconds
         work /= 60;                 // total number of mins
         long minutes = work % 60;   // remainder is minutes
         long hours = work / 60      // number of hours
      
         // now format and return - %ld is long decimal, %02ld is zero-padded two digit long decimal 
         return [NSString stringWithFormat:@"%ld:%02ld:%02ld", hours, minutes, seconds];
      }
      

      【讨论】:

        【解决方案3】:

        这是我项目中的代码:

        -(NSString*)timeLeftString
        {
            long seconds = [self msLeft]/1000;
            if( seconds == 0 )
                return @"";
            if( seconds < 60 )
                return [NSString stringWithFormat:
                    pluralString(seconds,
                    NSLocalizedString(@"en|%ld second left|%ld seconds left", @"")), seconds];
            long minutes = seconds / 60;
            seconds -= minutes*60;
            if( minutes < 60 ) 
                return [NSString stringWithFormat: 
                    NSLocalizedString(@"%ld:%02ld left",@""),
                    minutes, seconds];    
            long hours = minutes/60;
            minutes -= hours*60;
            return [NSString stringWithFormat:
                NSLocalizedString(@"%ld:%02ld:%02ld left",@""),
                hours, minutes, seconds];    
        }
        

        msLeft --- 我的函数以毫秒为单位返回时间 pluralString --- 我的函数根据值提供格式字符串的不同部分 (http://translate.sourceforge.net/wiki/l10n/pluralforms)

        函数为不同的计时器值返回不同的格式(还剩 1 秒、还剩 5 秒、还剩 2:34、还剩 1:15:14)。

        在任何情况下,长时间运行期间都应该可以看到进度不良

        再想一想:如果剩余时间“小”(不到一分钟?),可能不应该显示剩余时间 --- 只留下进度条以减少界面“视觉噪音”。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2011-06-21
        • 1970-01-01
        相关资源
        最近更新 更多