Objective-c 格式化距特定日期还剩的时间

发布于 11-29 16:13 字数 129 浏览 2 评论 0原文

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

I'm creating a countdown timer and I need to printout the time left (hour:minute:seconds) until a specific date. I've found how to get the time interval between Now and the target date but I don't know how to format the time interval as a string. Does NSDateFormater work on NSTimeInterval?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌2024-12-06 16:13:35

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];
}

NSTimeInterval is in seconds, use divide and remainder to break it up and format (code untested):

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];
}
赏烟花じ飞满天2024-12-06 16:13:35

您首先会比较两个 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;
}

You would first compare two NSDate objects to retrieve the difference in seconds between the two, the NSDate method you should use is

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

Then you could simply write a function to parse the seconds into hours/minutes/seconds, for example you could use this (untested):

-(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;
}
草莓味的萝莉2024-12-06 16:13:35

这是我的项目中的代码:

-(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 秒)。

无论如何,在长时间操作过程中,进度应该是可见的。

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

This is code from my project:

-(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 --- my function that returns time in milliseconds
pluralString --- my function that provides different parts of format string depending on the value (http://translate.sourceforge.net/wiki/l10n/pluralforms)

Function returns different format for different timer values (1 second left, 5 seconds left, 2:34 left, 1:15:14 left).

In any case, progress bad should be visible during long operation

One more thought: In case that time left is "small" (less then a minute?), probably time left should not be shown --- just progress bar left to reduce interface "visual noise".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文