1

I have been playing with NSDate and NSTimeInterval to try and get the time passed since a specific date, but I am not having much luck.

I want to specify an exact constant date (Jan 5th 2012 2PM) and calculate the time passed in Years, Hours, Min, Sec and display this on the screen as a counter.

It seems simple enough; however, browsing through code has yet to prove successful for me.

I also want to see total time in a years months days mins secs format. I appreciate any help, thanks!

This an example I have found that I don't know how to customize:

NSCalendar *c = [NSCalendar currentCalendar];
NSDate *d1 = [NSDate date];
NSDate *d2 = [NSDate dateWithTimeIntervalSince1970:1340323201];//2012-06-22
NSDateComponents *components = [c components:NSHourCalendarUnit fromDate:d2 toDate:d1 options:0];
NSInteger diff = components.minute;

NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit
4

1 回答 1

1

就像特坎扎基奇所说,

NSDateComponents* components = [c components:(NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:d1 toDate:d2 options:0] ;
NSLog(@"%ld years, %ld hours, %ld minutes, %ld seconds",  components.year, components.hour, components.minute, components.second) ;
于 2013-02-11T21:14:19.423 回答