显示一天,一小时前无法正常工作?
Show one day, one hour ago not working correctly?
我已经完成了这段代码
+ (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;
}
但它没有显示 correctly.If 我 post 服务器的任何记录,它显示 -1 秒前
如何用上面的代码解决这个问题。
我的服务器日期响应是 NSString
2015-09-16T14:16:49.187Z
我需要根据服务器日期差异计算当前时间。
为了避免这种情况,我手动添加了 +2 秒
temp=[NSString stringWithFormat:@"%ld secs ago",(long)components.second+2];
这不是完成此任务的好方法,请帮助我。
对于 Create Time Ago,我在应用程序委托中使用了 NSDate-TimeAgo 和 #import "NSDate+TimeAgo.h"
我创建了如下方法:
+(NSString*)GetTimeAgofromDate:(NSString*)DateString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:DateString];
NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate *dateInLocalTimezone = [dateFromString dateByAddingTimeInterval:timeZoneSeconds];
NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init];
[workingHoursFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSString *str = [workingHoursFormatter stringFromDate:dateInLocalTimezone];
//;
NSString *timeAgoFormattedDate = [dateInLocalTimezone timeAgo];
return timeAgoFormattedDate;
}
可能这对你有帮助。
我已经完成了这段代码
+ (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;
}
但它没有显示 correctly.If 我 post 服务器的任何记录,它显示 -1 秒前 如何用上面的代码解决这个问题。
我的服务器日期响应是 NSString
2015-09-16T14:16:49.187Z
我需要根据服务器日期差异计算当前时间。
为了避免这种情况,我手动添加了 +2 秒
temp=[NSString stringWithFormat:@"%ld secs ago",(long)components.second+2];
这不是完成此任务的好方法,请帮助我。
对于 Create Time Ago,我在应用程序委托中使用了 NSDate-TimeAgo 和 #import "NSDate+TimeAgo.h"
我创建了如下方法:
+(NSString*)GetTimeAgofromDate:(NSString*)DateString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:DateString];
NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate *dateInLocalTimezone = [dateFromString dateByAddingTimeInterval:timeZoneSeconds];
NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init];
[workingHoursFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSString *str = [workingHoursFormatter stringFromDate:dateInLocalTimezone];
//;
NSString *timeAgoFormattedDate = [dateInLocalTimezone timeAgo];
return timeAgoFormattedDate;
}
可能这对你有帮助。