I have this date:
2013-07-15T06:07:53-04:00
I use NSDateFormatter to convert this to NSDate:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
This works but it doesn't recognize the timezone. It returns:
2013-07-15 10:07:53 +0000
What am i doing wrong?
Thanks
The log output is correct. NSLogging an NSDate will always show that date/time in UTC (see how your output has +0000 instead of -04:00). NSDate objects represent a specific moment in time regardless of calendar and timezones. You use this "moment in time" in conjunction with a formatter (with a timezone set) in order to format that "moment in time" into a locale-specific time.
If you want to see log the output with your own timezone:
// pick a different timezone if necessary
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"%#", [formatter stringFromDate:momentInTime];
isn't the "-04:00" at the end of the string telling NSDateFormatter that this date is GMT-4 and thus, it's just converting it to GMT ?
If not, please provide the full initialization of your NSDateFormatter.
Try this instead:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
more info here
iPhone NSDateFormatter Timezone Conversion
5 ZZZZZ - heres a category I wrote with some sample of GMT to BST
https://github.com/clearbrian/NSDateFormatter_ISO_8601
Related
I want to make an NSDateFormatter object for this date format (for example):
2016-04-15 15:00:00
I've tried yyyy-mm-dd hh:mm:ss but then when I am trying to attach it to a date like that:
date = [formatter dateFromString:dateString];
It gives me nil.
what is the correct way to do it?
Thank you!
set date format as
2016-04-15 15:00:00
yyyy-MM-dd HH:mm:ss
your date string is 24 hour format
Here's all you need for date formatting:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
There's a list of link under the Fixed Format title where you can see in detail the different formats for whatever you want, for iOS7 and greater see this:
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
I'm currently pulling date/times from a Postgresql database via json and converting the date to an NSDate. I originally saved my date at Jan 15, 12PM PST. Can someone see where in the following process I go wrong ? It seems that on the iOS side its ignoring the timestamp
Originally the date in PostgreSql looks like:
2015-01-16 20:00:10+00
The date in JSON looks like:
"start_time" = "2015-01-16T20:00:10.000Z";
And when I convert this date using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z"];
I get
2015-01-17 04:00:10 +0000
Which also read:
"start_time" = "2015-01-16 20:00:10 PST in the debugger
The problem is you are quoting the timezone format specifier. You want:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Note the lack of quote before the Z.
I have searched over internet for a long time to get this but I can't find the solution. I have received a date string from web services as "22 May 2014", I have to convert into NSDate format for check it with current date. And I have to find out the date from web service is in future or in past time.
The actual problem is that when I convert this using
NSDate *date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM YYYY"];
date = [dateFormatter dateFromString:dateString];
But I get an entirely Different Date, Sample Input dateString:22 June 2014 and Output I get is 2013-12-21 18:30:00+0000
Please suggest any solutions.
Thanks in advance. :)
You're using YYYY, which doesn't mean what you think it means. From the TR35-31 documentation, Y is the symbol for "year in week-of-year calendars".
You want dd MMMM yyyy instead as your format string. Mixing week-of-year-based fields and regular day/month/year fields is a recipe for odd problems.
Additionally, you may well want to set the time zone in your formatter - if you're just parsing a date, then you should consider using UTC, and make sure that all your calculations and formatting/parsing use UTC.
(I suspect the issue here is that week-of-year hasn't been set, so is assumed to be 1... and the week-year 2014 started on December 30th. Then the day-of-month is set to 22 by the dd part, and then your time zone offset of UTC+05:30 is taken into account.)
Maybe it's an old question and I've searched on this site and found some similar questions, but I still cannot solve my problem. I have a NSString named gameDateTimeStr: "11/12/2013-10:00 PM" and I want to convert it to NSDate. I used the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy-HH:mm a"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *gameDateTime = [formatter dateFromString:gameDateTimeStr];
However, when printed on consolte, its description is: 2013-11-12 05:00:00 +0000
As you can see, the date is correct, but the time is wrong.
Please help me fix it.
There are one issues with you date formatter, first you are using 24 hour format, HH for the hours in the date format. But you example uses 12 hour format and hh should be used.
[formatter setDateFormat:#"MM/dd/yyyy-hh:mm a"];
Depending on you timezone offset the date is parsed correctly, since date printedby NSLog is is represented in GMT.
When you create a string form the date object use NSDateFormatter you timezone is used to calculate the correct time offset.
I'm having a problem with NSDate instance.
For the date I'm receiving 2012-08-16 00:00:00 +0000.
But I need to remove from the 00:00:00 +0000.
Is there any way to do that?
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *eventDate = [formatter dateFromString:currentDate]; // current Tile
NSLog(#"date %#",eventDate);
Use
NSLog(#"date %#",[formatter stringFromDate:eventDate]);
A NSDate is just a single point in time. It does not know how it's value should be formatted, that's what NSDateFormatter is for.
NSLog(#"date %#",eventDate); prints the default string representation of the date.
A date formatter converts strings into dates and dates into strings. An NSDate represents an point in time, regardless how you created it. In your code you seem to be thinking that the NSDate will "remember" that the formatter it came from didn't specify a time of day; it won't. When you call dateFromString: it will simply pick 00:00:00 (midnight) as the time.
If you need to work with calendar dates regardless of the time, you can either:
Use NSDate objects and ignore the time component. You will need to be wary of time zones, since midnight on August 15th in one time zone can be 11pm on August 14th in another.
Use a different data structure to store year, month, and day. NSDateComponents is a good candidate, or you could create your own.