时间跨度的智能格式化 [英] Smart Formatting of time span

查看:183
本文介绍了时间跨度的智能格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个方法将 NSTimeInterval (时间跨度以秒为单位)格式化为一个字符串,以便在大约10分钟前产生类似于1h,20min少于1分钟

   - (NSString *)formattedTimeSpan:(NSTimeInterval)interval; 

目标平台是iOS。示例代码是受欢迎的。

解决方案

这是NSDate的一个类别。它不是完全使用NSTimeInterval,内部很好:)我假设你正在使用时间戳。
$ b 头文件NSDate + PrettyDate.h

  @interface NSDate(PrettyDate)

- (NSString *)prettyDate;

$ end




$ b

实现NSDate + PrettyDate.m

  @implementation NSDate(PrettyDate)

- (NSString *)prettyDate
{
NSString * prettyTimestamp;

float delta = [self timeIntervalSinceNow] * -1;

if(delta <60){
prettyTimestamp = @just now;
} else if(delta <120){
prettyTimestamp = @一分钟前;
} else if(delta <3600){
prettyTimestamp = [NSString stringWithFormat:@%d分钟前,(int)floor(delta / 60.0)];
} else if(delta <7200){
prettyTimestamp = @1小时前;
} else if(delta <86400){
prettyTimestamp = [NSString stringWithFormat:@%d hours ago,(int)floor(delta / 3600.0)];
} else if(delta <(86400 * 2)){
prettyTimestamp = @一天前;
} else if(delta <(86400 * 7)){
prettyTimestamp = [NSString stringWithFormat:@%d days ago,(int)floor(delta / 86400.0)];
} else {
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];

prettyTimestamp = [NSString stringWithFormat:@on%@,[formatter stringFromDate:self]];
[formatter release];
}

return prettyTimestamp;
}


I need a method to format a NSTimeInterval (time span in seconds) into a string to produce something like "about 10 minutes ago", "1h, 20min", or "less than 1 minute".

-(NSString*) formattedTimeSpan:(NSTimeInterval)interval;

Target platform is iOS. Sample code is welcome.

解决方案

This is a category for NSDate. It's not exactly using an NSTimeInterval, well internally :) I assume you are working with timestamps.

Header file NSDate+PrettyDate.h

@interface NSDate (PrettyDate)

- (NSString *)prettyDate;

@end


Implementation NSDate+PrettyDate.m

@implementation NSDate (PrettyDate)

- (NSString *)prettyDate
{
    NSString * prettyTimestamp;

    float delta = [self timeIntervalSinceNow] * -1;

    if (delta < 60) {
        prettyTimestamp = @"just now";
    } else if (delta < 120) {
        prettyTimestamp = @"one minute ago";
    } else if (delta < 3600) {
        prettyTimestamp = [NSString stringWithFormat:@"%d minutes ago", (int) floor(delta/60.0) ];
    } else if (delta < 7200) {
        prettyTimestamp = @"one hour ago";      
    } else if (delta < 86400) {
        prettyTimestamp = [NSString stringWithFormat:@"%d hours ago", (int) floor(delta/3600.0) ];
    } else if (delta < ( 86400 * 2 ) ) {
        prettyTimestamp = @"one day ago";       
    } else if (delta < ( 86400 * 7 ) ) {
        prettyTimestamp = [NSString stringWithFormat:@"%d days ago", (int) floor(delta/86400.0) ];
    } else {
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];

        prettyTimestamp = [NSString stringWithFormat:@"on %@", [formatter stringFromDate:self]];
        [formatter release];
    }

    return prettyTimestamp;
}

这篇关于时间跨度的智能格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆