i have done with this code
+ (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;
}
But its not showing correctly.If i post any record to server,its shows -1 secs ago How to solve this with above code.
My server date response is NSString
2015-09-16T14:16:49.187Z
I need to calculate curent time with server date diffrence.
To avoid this i have added +2 secs manaually
temp=[NSString stringWithFormat:@"%ld secs ago",(long)components.second+2];
thats not good way to get this done, please help me with this.
For Create Time Ago thing i used NSDate-TimeAgo and #import "NSDate+TimeAgo.h"
in app delegate i create + method like following:
+(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;
}
Might be this is helpful to you.