priore

Time difference between two dates

Mar 14th, 2014
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @interface NSDate (TimeDifference)
  2.  
  3. + (NSDate*)dateWithString:(NSString*)dateString;
  4. + (NSString *)timeFormatted:(int)totalSeconds
  5.  
  6. @end
  7.  
  8. @implementation NSDate (TimeDifference)
  9.  
  10. + (NSDate*)dateWithString:(NSString*)dateString;
  11. {
  12.     NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  13.     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  14.     return [formatter dateFromString:dateString];
  15. }
  16.  
  17. + (NSString *)timeFormatted:(int)totalSeconds
  18. {
  19.     int seconds = totalSeconds % 60;
  20.     int minutes = (totalSeconds / 60) % 60;
  21.     int hours = totalSeconds / 3600;
  22.     return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
  23. }
  24.  
  25. @end
  26.  
  27. NSDate *date1 = [NSDate dateWithString:@"2014-03-14 17:10:00"];
  28. NSDate *date2 = [NSDate dateWithString:@"2014-03-15 05:00:10"];
  29. NSTimeInterval seconds = [date2 timeIntervalSinceDate:date1];
  30. NSLog(@"%@", [NSDate timeFormatted:seconds]);
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×