我已经完成了这段代码
+ (NSString *)relativeDateStringForDate:(NSString *)date
{
NSString *temp;
NSString* format = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:date];
// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];
// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
//calender settings
NSCalendarUnit units = NSDayCalendarUnit | NSWeekOfYearCalendarUnit |
NSMonthCalendarUnit | NSYearCalendarUnit | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ;
NSCalendar *cal = [NSCalendar currentCalendar];
//local dates
NSDate* datetime = [NSDate date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:format];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSDate* sysUTCDate = [dateFormatter dateFromString:dateTimeInIsoFormatForZuluTimeZone];
NSDateFormatter* formatterLocalFromUtc = [[NSDateFormatter alloc] init];
[formatterLocalFromUtc setDateFormat:format];
[formatterLocalFromUtc setTimeZone:[NSTimeZone localTimeZone]];
// Create local NSDate with time zone difference
NSDate* curreentDate = [formatterUtc dateFromString:[formatterLocalFromUtc stringFromDate:sysUTCDate]];
NSDateComponents *components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:curreentDate];
NSDate *today = [cal dateFromComponents:components1];
//server date
components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:localDate];
NSDate *thatdate = [cal dateFromComponents:components1];
// if `date` is before "now" (i.e. in the past) then the components will be positive
NSDateComponents *components = [[NSCalendar currentCalendar] components:units
fromDate:thatdate
toDate:today
options:0];
if (components.year > 0) {
temp=[NSString stringWithFormat:@"%ld years ago", (long)components.year];
} else if (components.month > 0) {
temp= [NSString stringWithFormat:@"%ld months ago", (long)components.month];
} else if (components.weekOfYear > 0) {
temp= [NSString stringWithFormat:@"%ld weeks ago", (long)components.weekOfYear];
} else if (components.day > 0) {
if (components.day > 1) {
temp= [NSString stringWithFormat:@"%ld day ago", (long)components.day];
} else {
temp=[NSString stringWithFormat:@"%ld day ago", (long)components.day];
}
} else {
if (components.hour!=0) {
if(components.hour==1)
{
temp= [NSString stringWithFormat:@"%ld hour ago",(long)components.hour];
}
else
{
temp= [NSString stringWithFormat:@"%ld hours ago",(long)components.hour];
}
} else if (components.minute!=0){
if(components.minute==1)
{
temp= [NSString stringWithFormat:@"%ld min ago",(long)components.minute];
}
else
{
temp=[NSString stringWithFormat:@"%ld mins ago",(long)components.minute];
}
}
else if (components.second!=0){
if(components.second==1)
{
temp= [NSString stringWithFormat:@"%ld sec ago",(long)components.second];
}
else
{
temp=[NSString stringWithFormat:@"%ld secs ago",(long)components.second];
}
}
}
return temp;
}
但它没有正确显示。如果我将任何记录发布到服务器,它会显示-1 秒前 如何用上面的代码解决这个问题。
我的服务器日期响应是NSString
2015-09-16T14:16:49.187Z
我需要用服务器日期差异计算当前时间。
为避免这种情况,我手动添加了 +2 秒
temp=[NSString stringWithFormat:@"%ld secs ago",(long)components.second+2];
那不是完成这项工作的好方法,请帮助我。