NSDate/NSDateFormatter question
NSDate/NSDateFormatter question
- Subject: NSDate/NSDateFormatter question
- From: Donald Hall <email@hidden>
- Date: Sat, 24 Mar 2012 21:14:24 -0600
Can anyone explain what is going on here:
NSDate *now = [NSDate date];
NSLog(@"now is %@", now);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; // this is MDT for me
[dateFormatter setDateFormat:@"h:mm a"];
NSString *newDateString = [dateFormatter stringFromDate:now];
NSLog(@"stringFromDate=%@", newDateString);
NSDate *newDateFromString = [dateFormatter dateFromString:newDateString];
NSLog(@"new date from string=%@", newDateFromString);
newDateString = [dateFormatter stringFromDate:now];
NSLog(@"2nd stringFromDate=%@", newDateString);
result:
2012-03-24 20:57:18.976 calendardatetest[77052:707] now is 2012-03-25 02:57:18 +0000
2012-03-24 20:57:18.977 calendardatetest[77052:707] stringFromDate=8:57 PM
2012-03-24 20:57:18.978 calendardatetest[77052:707] new date from string=1970-01-02 03:57:00 +0000
2012-03-24 20:57:18.979 calendardatetest[77052:707] 2nd stringFromDate=8:57 PM
"now" is what I expected as the test was done at 8:57 PM local time, which being MDT is 6 hours behind GMT
When I go in reverse using just the time string, the time for the new date object seems to ignore Daylight Saving Time - it gives 3:57 on the next day instead of 2:57.However, if I create a new date string from this I get the correct local time again, with DST taken into account. (I don't care about the y-m-d part of the date object in my application.)
It seems to give back the correct local time even though the GMT time differs by one hour in the two cases and the date formatter is the same.
Thanks for any insight - the DST- standard time thing is making me nervous.
Don
------------
Don Hall
Apps & More Software Design, Inc.
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden