【问题标题】:How to get "days ago, hours ago" time format in iOS?如何在 iOS 中获取“几天前,几小时前”的时间格式?
【发布时间】:2016-01-20 07:31:07
【问题描述】:

我想在 iOS 中获得几小时前、几天前的时间/日期格式。但我得到了错误的数据。例如,如果时间是:

2015-10-21 03:43:20

然后根据下面的代码显示 6 小时前。我在 php 中使用 Now() 函数来保存时间/日期,那么如何显示不同国家/地区的确切时间?

+(NSString*)HourCalculation:(NSString*)PostDate
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormat setTimeZone:gmt];
    NSDate *ExpDate = [dateFormat dateFromString:PostDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
    NSString *time;
    if(components.year!=0)
    {
        if(components.year==1)
        {
            time=[NSString stringWithFormat:@"%ld year",(long)components.year];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld years",(long)components.year];
        }
    }
    else if(components.month!=0)
    {
        if(components.month==1)
        {
            time=[NSString stringWithFormat:@"%ld month",(long)components.month];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld months",(long)components.month];
        }
    }
    else if(components.week!=0)
    {
        if(components.week==1)
        {
            time=[NSString stringWithFormat:@"%ld week",(long)components.week];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
        }
    }
    else if(components.day!=0)
    {
        if(components.day==1)
        {
            time=[NSString stringWithFormat:@"%ld day",(long)components.day];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld days",(long)components.day];
        }
    }
    else if(components.hour!=0)
    {
        if(components.hour==1)
        {
            time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
        }
    }
    else if(components.minute!=0)
    {
        if(components.minute==1)
        {
            time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
        }
    }
    else if(components.second>=0)
    {
        if(components.second==0)
        {
            time=[NSString stringWithFormat:@"1 sec"];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
        }
    }
    return [NSString stringWithFormat:@"%@ ago",time];
}

【问题讨论】:

  • 你搜索了吗?我确定以前有人问过这个问题。

标签: ios objective-c timezone


【解决方案1】:

一些观察:

  1. 首先,如果你使用NSDateComponentsFormatter,你可以大大简化这段代码,例如

    NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
    formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
    formatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSString *elapsed = [formatter stringFromDate:someDate toDate:[NSDate date]];
    

    这会生成一个字符串,表示两个 NSDate 对象之间的时间,如下所示:

    5 年 9 个月 20 天 3 小时 48 分 56 秒

    或者您可以指定最大单位数,它只会显示 n 个最大的。例如:

    formatter.maximumUnitCount = 2;
    

    这将产生:

    5 年 10 个月

  2. 注意常量的使用,例如NSCalendarUnitYear 而不是NSYearCalendarUnit

  3. 如果经过时间关闭,最常见的情况是时间字符串中的时区不正确。避免此类问题的最简单方法是确保您从服务器收到的字符串位于已知时区(例如 GMT)或时间字符串包含时区。我们通常会在 Web 服务中使用 ISO 8601/RFC 3339 日期格式并相应地调整我们的日期格式器(请参阅 Apple Technical Q&A 1480,其中讨论了一些微妙的问题)。

【讨论】:

    【解决方案2】:

    您可以通过以下链接使用从 github 下载的 NSDate+NVTimeAgo 类,, https://github.com/nikilster/NSDate-Time-Ago 我相信它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多