last day (15 days later) » 

5:12 PM
-2
Q: iOS App Bug: NSDateFormatter returns 1 January, 2001 as the date

Pritesh DesaiI'm storing values in GMT format on a server. I download them on the device and convert it to the users local time via the following function: - (void)convertTimeZone { //Converts the match timing from GMT to the users local time NSString *serverDateString = [NSString stringWithFormat:@"...

 
Without seeing your inputs and outputs it's impossible to locate all the places where you might have screwed up. (BTW, -1 for declaring an "iOS bug" when the odds of that are vanishingly small.)
Your code makes absolutely no sense -- you convert a string to NSDate, convert that back to string, then convert to NSDate again, all with the same date formatter, and with no changes to the date formatter setup. What do you expect to change?
(And how do you expect the above to convert from GMT to local time?)
 
@HotLicks I've checked things to isolate the problem. Getting JSON values to the device and storing it is working fine. I know this because few users are facing problem with the time only and not other text data.
@HotLicks The above code does convert the date-time from GMT to users local time. 1 in a 100 people found that it did not work. I thought it was a bug. Hopefully bdesham's answer will work.
 
Then maybe you're right -- maybe iOS is broken. Because I can't seen how the above does a timezone conversion.
 
stringWithFormat:@"%@-%@-%@ %@:%@ GMT. Notice the GMT part.
 
Ah, yes. But why convert back and forth twice? Once you've got it into an NSDate there's no need for further conversion.
 
5:14 PM
I've stored date on the server as separate values - 'hrs', 'mins', 'date'...
So I download them and create a date
which is in GMT
Then I convert it to the users local time
 
So? Why convert the NSDate back to a string and then to an NSDate again? (Remember, NSDate is ALWAYS GMT.)
 
and then break it down to individual strings
date, hrs, min
 
NSDate is always GMT (unless you screw up). Timezone gets applied by NSDateFormatter and NSCalendar.
 
ok, so what would be the best way to write this function?
 
serverDate and localDate will contain the exact same value.
 
5:17 PM
so how do I get the local date accurately?
 
Just use serverDate directly, would be a first cut at it. And specify locale as stated elsewhere.
 
ok, thanks
 
Another way to do it would be to use NSCalendar the other direction, to create the NSDate, then back the way you already did. Sent the NSCalendar object to be GMT on the first conversion, and local time coming back. No way (that I know of) to get bit by the locale "feature" doing it that way, and in theory a hair faster than using NSDateFormatter.
Oops -- "Set the NScalendar object to be GMT...:"
 

last day (15 days later) »