Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please try this method:</p> <pre><code>-(NSString *)durationbetweendate:(NSString *)strdate1 date2:(NSString *)strdate2{ NSString *stringtime = strdate1; [NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; [dateFormat1 setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *sourceDate1 = [dateFormat1 dateFromString:strdate1]; NSLog(@"%@",[NSDate date]); NSTimeInterval timeDiff ; if ([[NSDate date] laterDate:sourceDate1]) timeDiff = [[NSDate date] timeIntervalSinceDate:sourceDate1]; else timeDiff = [sourceDate1 timeIntervalSinceDate:[NSDate date]]; NSLog(@"second %f",timeDiff); float years = floor(timeDiff/(24*60*60*365)); if (years &gt;0) { if (years == 1) return [NSString stringWithFormat:@"%d Year ago", (int)years]; else return [NSString stringWithFormat:@"%d Years ago", (int)years]; } else{ float months = floor(timeDiff/(24*60*60*30)); if (months&gt;0) { if (months == 1) return [NSString stringWithFormat:@"%d Month ago", (int)months]; else return [NSString stringWithFormat:@"%d Months ago", (int)months]; } else{ float days = floor(timeDiff/(24*60*60)); if (days&gt;0) { if (days == 1) return [NSString stringWithFormat:@"%d Day ago", (int)days]; else return [NSString stringWithFormat:@"%d Days ago", (int)days]; } else{ float hours = floor(timeDiff/(60*60)); if (hours&gt;0) { float minutes = 0; minutes = floor(timeDiff/60) - (hours * 3600); float seconds = 0; seconds = round(timeDiff - (minutes * 60) - (hours * 3600)); if (hours==1) return [NSString stringWithFormat:@"%d Hour ago", (int)hours]; else return [NSString stringWithFormat:@"%d Hours ago", (int)hours]; } else { float minutes = floor(timeDiff/60); if (minutes &gt;0) { if (minutes == 1) return [NSString stringWithFormat:@"%d Minute ago", (int)minutes]; else return [NSString stringWithFormat:@"%d Minutes ago", (int)minutes]; } else { float seconds = round(timeDiff - (minutes * 60)); if (seconds == 1) return [NSString stringWithFormat:@"%d Second ago", (int)seconds]; else return [NSString stringWithFormat:@"%d Seconds ago", (int)seconds]; } } } } } return stringtime; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload