【问题标题】:NSDate return hour/day/weekNSDate 返回小时/天/周
【发布时间】:2013-03-22 11:38:07
【问题描述】:

Instagram程序中的快照,日期显示为(例如)2小时前/3天前/1周前..我有“2013-03-20 16:02:48”..我怎样才能得到字符串“ 2 天前”?

【问题讨论】:

  • 这个问题与 xcode 无关,因为代码是用 Xcode、vi 编写的还是使用电报输入的都没有关系。请仅在您想了解有关 xcode 编辑器本身的情况下使用 Xcode 标签。
  • NSDateComponents 对象,不适用于 iOS?我已经用这个对象解决了这个问题。所以不要变得聪明
  • 你在说什么?
  • 所以你显然没有明白我在说什么:Xcode 是一个编辑器,不多不少。它用于编写程序,但它不是一种编程语言(即objective-c)。它不是您在编程语言(即 Cocoa/Cocoa-Touch)中使用的类和函数的集合。它不是操作系统(即 Mac OS X/iOS)。 Xcode 也不是——也不包含——NSDateComponents。这属于框架。您可以在任何其他编辑器中使用 Objective-C、cocoa-touch 和 NSDateComponents 编写 iOS 程序。没关系。 Xcode 是无关紧要的,你的标签是在制造噪音
  • 到 Xcode 标签上,这意味着正确标记 xcode 的帖子会丢失。所以请变得聪明——至少更聪明。不了解他们的工具的开发人员是一种负担。

标签: ios objective-c cocoa nsdate


【解决方案1】:

考虑使用NSCalendarNSDateComponents 来计算差异并与最终用户相似。

【讨论】:

    【解决方案2】:

    有一个开源项目

    https://github.com/billgarrison/SORelativeDateTransformer

    来自其文档:

    例如,当当前日期时间为 2010-12-05 11:30 时,则
    ... 2010-12-05 11:00 转换为“30 分钟前”
    ... 2010-12-01 11:00 转换为“5 天前”
    ... 2010-12-25 08:00 转换为“2 周内”

    // Display a file's creation date relative to today's date
    NSURL *someFilePath = ...;
    NSDictionary *attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:NULL];
    
    NSDate *dateModified = [attribs fileModificationDate];
    
    // fileModificationDate is 2010-10-01 12:00:00TZ; 
    // Date now is 2010-10-30 12:00:00TZ
    
    SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init];
    NSString *relativeDate = [relativeDateTransformer transformedValue: dateModified];
    
    NSLog (@"This file was modified %@.", relativeDate); // ==> "This file was modified 3 weeks ago."
    

    【讨论】:

    • 好一个。但请注意,其许可需要署名。
    • 不是,如果您只是查看一些实现细节。无论如何:在信用到期时给予信用......
    • 我不介意归属。相反,我的评论是为了提醒大家尊重许可,因为署名是最容易“忘记”的要求之一。
    • 啊,好的。我很乐意在 Helvetica Neue all caps 24.0 中添加归属,只是为了本地化。
    【解决方案3】:

    也许某处有一个图书馆可以为你做这件事。

    但这并不难。可以将 NSDate 分解为其组件。然后很容易比较它们并编写一些 if then else 代码来涵盖所有需要的情况。

    这是我的一个项目中的一个示例,我将日期解析为过去 7 天内的适当工作日:

    NSString *resultDateString;
    
    // First, reduce the two dates we want to compare to the scope of days by erasing hours, minutes and seconds
    
    unsigned int flagA = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *compsA = [[NSCalendar currentCalendar] components:flagA fromDate:myObjectsDate];
    [compsA setHour:0];
    [compsA setMinute:0];
    [compsA setSecond:0];
    NSDate *checkDate = [[NSCalendar currentCalendar] dateFromComponents:compsA];
    
    unsigned int flagB = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *partsB = [[NSCalendar currentCalendar] components:flagB fromDate:[NSDate date]];
    [compsB setHour:0];
    [compsB setMinute:0];
    [compsB setSecond:0];
    NSDate *nowDate = [[NSCalendar currentCalendar] dateFromComponents:compsB];
    
    // Get the interval between the two dates, interval is still in seconds, but since it is calculated using days only, it will be precisely 0 if the day is the same    
    
    NSTimeInterval interval = [searchDate timeIntervalSinceDate:nowDate];
    
    // check if the objects date is today. If it isn't, find out which day it is and name it appropriately
    
    if (interval == 0) {
    
        resultDateString = [NSString stringWithString:@"Today"];
    
    } else {
    
       unsigned int flagC = NSWeekdayCalendarUnit;
       NSDateComponents *compsC = [[NSCalendar currentCalendar] components:flagC fromDate:myObjectsDate];
    
       switch ([compsC weekday]) {
           case 1:
               resultDateString = @"Sunday";
               break;
           case 2:
               resultDateString = @"Monday";
               break;
           case 3:
               resultDateString = @"Tuesday";
               break;
           case 4:
               resultDateString = @"Wednesday";
               break;
           case 5:
               resultDateString = @"Thursday";
               break;
           case 6:
               resultDateString = @"Friday";
               break;
           case 7:
               resultDateString = @"Saturday";
               break;
    
           default:
               break;
       }
    
    }
    

    这只是一个摘录,请注意,此代码 sn-p 不检查间隔是否长于例如 7 天,因为我在代码中以不同方式涵盖了此类情况。但我认为它会让你对如何做到这一点有一个基本的了解。

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2022-01-02
      • 2010-12-25
      • 1970-01-01
      • 2011-12-03
      • 2011-08-03
      相关资源
      最近更新 更多