Using NSDate is tricky to me. I know it represents a specific point in time, not tied to a specific time zone or locale. I’ve also read a lot fo the docs on NSDateFormatter, NSCalendar, NSDateComponents, NSlocale, and NSTimeZone.
I’m trying to do calculations based on two specific Pacific time zones. For example, I want to be able to count the number of days from date A to date B. I don’t want to ‘hack’ it by parsing strings, as I want things like DST to be calculated as well.
Can anyone recommend a way to do this?
Edit: Found solution:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"PST"];
[calendar setTimeZone:zone];
NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
date = [calendar dateFromComponents:comp];
[calendar release];
This results in a date localized to Pacific time. I know this causes problems with different locales, but my app is unique, and this is justified.
I can then compare dates using the division by 86400 as was posted.
Although, it seems like overkill – a lot of extra work – perhaps there is a shorter way to get a date with a specific time zone?
You should create two
NSDateinstances that represent the two dates you want to compare. To do this, you can set upNSDateComponentsinstances and set not only their day/month/year/hour but also their time zone (-[NSDateComponents setTimeZone:]). Then call-[NSDateComponents date]to convert them toNSDate.[date1 timeIntervalSinceDate:date2]then gives the difference (in seconds) between the two dates. Divide the result by 86,400 to get the number of days.Alternatively, you can call
[NSCalendar components:fromDate:toDate:options:]with theNSDateComponentinstances directly to get anNSDateComponentswith the difference between the two.