I have a NSDate object. Let’s say it represents “1-10-2011”
NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"];
That date translates into “2011-09-30 22:00:00” because of my timezone.
Question: How do I get a new Date object representing “2011-10-01 00:00:00” in my local timezone?
NSDate only represents an absolute point in time. It has no concept of timezone or calendar. When you create a NSDate instance it is just a number of seconds since January 1st 2001 GMT! It does not matter if you are in New York, Tokyo, Barcelona or Jerusalem.
At your example, you instance the NSDate based on GMT, but
[date description](used inNSLog) translates it into your local time. There you have the mismatch.So there are two parts to consider:
1. NSDate creation using NSCalendar and NSTimeZone
If you are creating a date manually you should specify the calendar (2012 in Gregorian, but 5772 in Hebrew) and time zone (22PM London time, but 7AM Sydney time).
At this point date stores the exact point in time (in seconds) representing the current calendar.
2. NSDate output using NSDateFormatter
For a controlled output of your NSDate you need NSDateFormatter, which is used to convert dates into strings.
Based on Apple NSDateFormatter Class Reference documentation
This is specially important for the output, which is different for every locale. By default NSDateFormatter observes the current user’s locale settings. So the same NSDate could be
22.11.2011 18:33:19, orNov 22, 2011 6:33:19 PM, or2011-11-22 下午6:33:19or even२२-११-२०११ ६:३३:१९ अपराह्, all for the same input and with the same code.And the code:
Or you could transform it using the class method localizedStringFromDate:dateStyle:timeStyle:
I hope this clarifies the problem.