NSDate返回小时/天/周

时间:2013-03-22 11:38:07

标签: ios objective-c cocoa nsdate

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

3 个答案:

答案 0 :(得分:1)

考虑使用NSCalendarNSDateComponents计算差异,并与最终用户进行类似的宣传。

答案 1 :(得分:1)

有一个开源项目

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."

答案 2 :(得分:0)

也许有一个图书馆会为你做这件事。

但无论如何,这并不难。可以将NSDate分解为其组件。然后很容易比较它们并编写一些if else else代码来覆盖所有想要的案例。

以下是我的一个项目中的一个示例,其中我将日期解析为过去七天内的相应工作日:

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

}

这只是一个摘录,请注意,此代码段不会检查间隔是否超过7天,例如,因为我在代码中以不同的方式覆盖了这些情况。但我认为这将为您提供如何做到这一点的基本想法。