문제

I am trying to get the time difference between two NSDates.

NSDate *d1 = somedate;
NSDate *d2 = someOtherdate;
NSTimeInterval sec = [d2 timeIntervalSinceDate:d1];

How accurate is NSTimeInterval? Does it take care of the variation in number of days in a month, for example if the number of days is 28,29,30 or 31? Also time zone difference?

도움이 되었습니까?

해결책 4

How accurate is NSTimeInterval?

It's accurate to milliseconds (if not more).

Does it take care of the variation in number of days in a month, for example if the number of days is 28,29,30 or 31?

Yes, the difference will be in seconds. NSDate deals with actual days in a month as well as leap years and daylight savings, etc.

Also time zone difference?

NSDate values are always stored in UTC so your NSDate objects are always in the same timezone. There is nothing to deal with for this.

다른 팁

How accurate is NSTimeInterval

On the platforms I know they have Cocoa, NSTimeInterval is typedeffed to double,

Does it take care of the variation in number of days in a month?

It's not NSTimeInterval that does that, it's a type. It's the various classes (NSDate, whatever) that take care of all these deviances. And they do it really well.

NSTimeInterval is in fact just a typedeffed double. Because NSDate represents a single point in time (independent of time zone, calendar, etc.), NSTimeInterval represents the number of seconds between the two dates (points in time).

NSTimeInterval is actually just a typedef for double.

NSDate encapsulates a time interval and provides an interface to interact with it.

If you want that date to be referenced to our actual calendar, you have to use the NSCalendar and NSDateComponents classes.

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

Then you can get data out of the components object:

components.day
components.week
// etc

Assume the Mayan culture never went extinct. This right instant in time would be represented by the same NSDate object, but through a different NSCalendar (like a NSMayanCalendar) you would get a completely different representation of that date.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top