You can use NSDateComponents
and NSCalendar
to create a new date. From the Date and Time Programming Guide
Creating Dates with Time Zones
Time zones play an important part in determining when dates take
place. Consider a simple calendar application that keeps track of
appointments. For example, say you live in Chicago and you have a
dentist appointment coming up at 10:00 AM on Tuesday. You will be in
New York for Sunday and Monday, however. When you created that
appointment it was done with the mindset of an absolute time. That
time is 10:00 AM Central Time; when you go to New York, the time
should be presented as 11:00 AM because you are in a different time
zone, but it is the same absolute time. On the other hand, if you
create an appointment to wake up and exercise every morning at 7:00
AM, you do not want your alarm to go off at 1:00 PM simply because you
are on a business trip to Dublin—or at 5:00 AM because you are in Los
Angeles.
NSDate objects store dates in absolute time. For example, the date
object created in Listing 16 represents 4:00 PM CDT, 5:00 EDT, and so
on.
Listing 16 Creating a date from components using a specific time zone
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init]; [timeZoneComps setHour:16];
//specify whatever day, month, and year is appropriate
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
If you need to create a date that is independent of timezone, you can store the
date as an NSDateComponents object—as long as you store some reference
to the corresponding calendar.
In iOS, NSDateComponents objects can contain a calendar, a timezone,
and a date object. You can therefore store the calendar along with the
components. If you use the date method of the NSDateComponents class
to access the date, make sure that the associated timezone is
up-to-date.
Anyway, keep in mind that a date is a unique point in time. What you display to the user is different based on their locale and time zone.