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).
Related
I'm developing an application and I need to parse a Tweet timestamp that actually is a string like this:
"Mon, 17 Oct 2011 10:20:40 +0000"
However what I need it is only the time. So what I'm trying to get it is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *date = [dateFormatter dateFromString:timestamp];
but when I try to print the Date with NSLog the output is (null) and I don't understand why. What I need, if it is possible, is to create a date object only with the time. Indeed later on I need to compare different dates but I care only about the time. It is not important if the date are different because of the day, month or year, the important is that I can compare them with the "timeIntervalSinceDate" to get the difference in seconds.
Any help will be really appreciated.
Thanks
Ale
NSDATE is al full date object, it needs a date en time.
You can use NSDateFormatter to only display the time, but you will need to parse the full string to get the NSDate object.
Here some code you can use:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Mon, 11 Jul 2011 00:00:00 +0200
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"EN"] autorelease]];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZZ"];
NSDate *date = [dateFormatter dateFromString: timestamp];
[dateFormatter release], dateFormatter = nil;
You need to set the local to make sure it read the timestamp in the correct language.
Also alloced the dateFormatter outside any loops and release it after you're done with the loop.
Look at NSDateComponents. You should be able to create a date from components. You'll have to parse the timestamp yourself, though (which should be easy — just split on the colon and grab the number from each component).
Why would this code be giving me GMT? (I am in US Mountain Time)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *now = [NSDate date];
NSString *storeTime = [dateFormatter stringFromDate:now];
An NSDate represents a concrete point in time, regardless of the timezone. Put another way, an NSDate does not have a timezone. Timezones are only relevant when you want to display the date to the user. So 9:30pm in Mountain Time is 3:30am (+1 day) in GMT (assuming a 6 hour time difference).
NSDate, since it does not have a timezone, must pick one when producing a human-readable version to return as its -description. To make things simple, it always returns a date formatted in the GMT time zone. If you would like the date formatted to be in a different timezone, you can set the -timezone property of an NSDateFormatter, and then convert the date into a string using the -stringFromDate: method.
Yes, turns out this was a bug in Apple's Numbers on the Mac. Numbers was not interpreting the date string, or rather it was adding the time offset. The NSDate string, after formatting, was correct all along.
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 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.
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?