I am having a problem with my datetime field getting changed by the time zone.
The incoming datetime is -- 2010-12-28 19:10:00
only when I use get date from string it comes out as -- 2010-12-29 00:10:00 +0000
I am doing
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[[dateFormatter locale] localeIdentifier];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
p.date = [dateFormatter dateFromString:[values objectAtIndex:6]];
It's like it it applies GMT and adds 5 hours to my date time. How can I stop this from happening.
thanks
Cheryl
Assuming your local time is GMT-5, this is perfectly fine. 2010-12-29 00:10:00 +0000 and 2010-12-28 19:10:00 -0500 both identify exactly the same point in time. And that is all an NSDate represents: a single point in time. The time zone in which you display it is determined when you create another date formatter to convert the date back to a string for display.
Related
I'm trying to convert a string to an NSDate using the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date= [dateFormatter dateFromString:#"2013-03-10T02:00:00"];
It works fine for all hours except 02:00:00, shown above, which returns nil.
Any ideas?
This is because of the Daylight Savings Time that took place in the US on 10th March 2013
The System time directly jumps from 1:59 AM to 3:00 AM..
You lose an hour that you had gained on November when the DST came into effect
NSLog(#"%#", self.departDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSLog(#"%#", [dateFormat stringFromDate:self.departDate]);
[dateFormat release];
So I have this code, which returns this in the console:
2011-06-09 00:00:00 +0000
06/08/2011
Any ideas why this is happening? As you can see, my self.departDate object returns 2011-06-09 as it's date. When I try to use date formatter to convert, I lose a day WTF?
The first log corresponds to a GMT date. However when you create an NSDateFormatter instance, it will have a default timezone set to the user's timezone which I am guessing is probably somewhere west of Greenwich. If you want the time to remain in GMT, you will have to add this line,
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
I want current date and time in PST. I used this code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"String:%#",timeStamp);
It returns correct date and time in PST in string form but I want NSDate in PST. So when I change NSString to NSDate like this:
NSDate *currentPST = [dateFormatter dateFromString:timeStamp];
NSLog(#"currentPST Date:%#",currentPST);
It returns date in GMT. I have done R&D but all in vain.Output is:
String:2011-05-18 22:28:54 PDT
currentPST Date:2011-05-19 05:28:54 +0000
Can anyone suggest a solution please.
Thanks in advance
In Cocoa, NSDate is an abstract representation of a date with no time zone information applied.
Whenever you print a NSDate object, it will print the date value corresponds to the default timezone(your device timezone). Your device timezone is GMT thats why you get the value like that. If you look into that deeply, both the time where same, but the timezone varies.
I am parsing the following string format: "29/09/2010 12:45:00" with the following code:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *dateTime = [[df dateFromString:dateAndTime]];
But the NSDate object then contains the following: 2010/09/29 11:45:00
Does anyone have any idea why it is taking off an hour?
Many thanks for any help.
What time zone are you in? Unless otherwise specified, NSDateFormatter assumes that the string it's parsing is in the GMT +0000 timezone. If you're in GMT -0100 (one hour west of GMT), then it's going to show the time as one hour off (since you're ignoring the time zone when printing).
I have a very strange date format coming to me via JSON. e.g. - "July, 18 2010 02:22:09"
These dates are always UTC. I'm parsing the date with NSDateFormatter, and setting the timeZone to UTC...
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[inputFormatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:dtStr];
However, the date when logged is appearing with the offset of my device...
"2010-07-18 02:22:09 -0600"
What am I doing wrong here?
I think this is because your NSDate isn't using the UTC timezone. The docs say this about the description method of NSDate:
A string representation of the receiver in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from GMT (for example, “2001-03-24 10:45:32 +0600”).
So, you aren't really doing anything wrong. NSDate is behaving as expected. What are you trying to accomplish? Do you need the date in a string? Are you doing date comparison?