字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时分秒……


= ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
NSString *dateStr= @"2013-08-13 20:28:40" ; //传入时间
//将传入时间转化成需要的格式
NSDateFormatter *format=[[ NSDateFormatter alloc ] init ];
[format setDateFormat : @"yyyy-MM-dd HH:mm:ss" ];
NSDate *fromdate=[format dateFromString :dateStr];
NSTimeZone *fromzone = [ NSTimeZone systemTimeZone ];
NSInteger frominterval = [fromzone secondsFromGMTForDate : fromdate ];
NSDate *fromDate = [fromdate dateByAddingTimeInterval : frominterval ];
NSLog( @"fromdate=%@" ,fromDate);
[format release ];
//获取当前时间
NSDate *date = [ NSDate date ];
NSTimeZone *zone = [ NSTimeZone systemTimeZone ];
NSInteger interval = [zone secondsFromGMTForDate : date ];
NSDate *localeDate = [date dateByAddingTimeInterval : interval ];
NSLog( @"enddate=%@" ,localeDate);
 
 
double intervalTime = [fromDate timeIntervalSinceReferenceDate ] - [localeDate timeIntervalSinceReferenceDate ];
 
long lTime = ( long )intervalTime;
NSInteger iSeconds = lTime % 6 0 ;
NSInteger iMinutes = (lTime / 6 0 ) % 6 0 ;
NSInteger iHours = (lTime / 3 6 0 0 )$;
NSInteger iDays = lTime/ 6 0 / 6 0 / 2 4 ;
NSInteger iMonth = lTime/ 6 0 / 6 0 / 2 4 / 1 2 ;
NSInteger iYears = lTime/ 6 0 / 6 0 / 2 4 / 3 8 4 ;
 
NSLog( @"相差M年d月 或者 d日d时d分d秒" , iYears,iMonth,iDays,iHours,iMinutes,iSeconds);

你可能感兴趣的:(Date,360)