我从xml解析了NSString
,其中包含日期,例如
Mon May 10 23:26:22 +0000 2010
我很难将NSString“date”转换为更高效的东西,例如
5小时前
昨天
一周前
如果有人能指出我正确的方向如何尝试这一点,将不胜感激。
答案 0 :(得分:1)
我确切地知道你的意思。试试这个,我一直在使用这个&它对我来说很完美 -
用法 - [Utils humanDates:timeSinceEpoch];
+ (NSString *)humanDates:(NSString *)origDateInEpochTime
{
if([origDateInEpochTime length]<= 0)
return @"never";
NSDate *convertedDate = [NSDate dateWithTimeIntervalSince1970:[origDateInEpochTime doubleValue]/1000];
NSDate *todayDate = [NSDate date];
double ti = [convertedDate timeIntervalSinceDate:todayDate];
ti = ti * -1;
if(ti < 1)
{
return @"never";
}
else if(ti < 60)
{
return @"less than a minute ago";
}
else if(ti < 3600)
{
int diff = round(ti/60);
if(diff == 1)
return [NSString stringWithFormat:@"%d minute ago", diff];
else
return [NSString stringWithFormat:@"%d minutes ago", diff];
}
else if(ti < 86400)
{
int diff = round(ti/60/60);
if(diff == 1)
return [NSString stringWithFormat:@"%d hour ago", diff];
else
return [NSString stringWithFormat:@"%d hours ago", diff];
}
else if(ti < 2629743)
{
int diff = round(ti/60/60/24);
if(diff == 1)
return [NSString stringWithFormat:@"%d day ago", diff];
else
return [NSString stringWithFormat:@"%d days ago", diff];
}
else if(ti < 31104000)
{
int diff = round(ti/60/60/24/30);
if(diff == 1)
return [NSString stringWithFormat:@"%d month ago", diff];
else
return [NSString stringWithFormat:@"%d months ago", diff];
}
else if(ti < 311040000)
{
int diff = round(ti/60/60/24/30/12);
if(diff == 1)
return [NSString stringWithFormat:@"%d year ago", diff];
else
return [NSString stringWithFormat:@"%d years ago", diff];
}
else
{
int diff = round(ti/60/60/24/30/12/10);
if(diff == 1)
return [NSString stringWithFormat:@"%d decade ago", diff];
else
return [NSString stringWithFormat:@"%d decades ago", diff];
}
}
答案 1 :(得分:1)
我这样做
首先我写下这样的功能
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:0]]];
NSInteger nDiffDate = [self howManyDaysHavePast:dateSelected today:[NSDate date]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:dateSelected];
[dateFormatter release];
[dateForm release];
if (nDiffDate ==0) {
dateLbl.text = [[NSString stringWithFormat:@"%@",[recentHisotryArr objectAtIndex:0]]substringFromIndex:11];
}
else if(nDiffDate==1)
{
dateLbl.text = @"Yesterday";
}
else if(nDiffDate==2)
{
dateLbl.text = dayName;
}
else if(nDiffDate==3)
{
dateLbl.text = dayName;
}
else if(nDiffDate==4)
{
dateLbl.text = dayName;
}
else if(nDiffDate==5)
{
dateLbl.text = dayName;
}
else if(nDiffDate==6)
{
dateLbl.text = dayName;
}
-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today
{
NSDate *startDate = lastDate;
NSDate *endDate = today;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
int days = [components day];
return days;
}
它对我来说就像iphone拨号应用程序上的那天提醒一样