Formatting a date from a string - objective-c

I have looked at a number of other solutions on here, but can't seem to get this to work for my case.
I am consuming an API, from which some parts will be saved to core data, however the date format in the API doesn't match that of which core data expects,
The date that I am getting is in the format:
Jun 28, 2013 5:51:28 PM
I need to be able to sort my entity by this date in order to display the latest items. I tried the following format, but I can't seem to get a result that works, any ideas on how I can achieve this?
NSDate *createdDate = [key objectForKey:#"createdDate"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"mm/dd/yyyy hhmmss"];
NSDate *date = [dateFormat dateFromString:createdDate];

createdDate can't be an NSDate. If it was your code would crash rather than just not working. It must be an NSString.
NSString *createdDate = [key objectForKey:#"createdDate"];
Next, the date string you say you receive is Jun 28, 2013 5:51:28 PM which doesn't even slightly match the format you're trying to use of mm/dd/yyyy hhmmss. For a start the format has slashes and the date string doesn't. The format must match the string construction exactly. Read the date formatter format spec again and modify your format. I don't have it to hand but it will be something like MMM dd, yyyy H:mm:ss a (don't just use that format, check it first).

Related

IOS6 Incorrect TimeZone while converting date from string to NSDate

My date is in the format
2013-07-16T07:40:36.939-04:00
When I convert it into a NSDate
the date is in the format IST or GMT +5:30 as I am in India. How should I make it use -4:00 as the timezone and display EST or PDT as per the number. if I use zzz, it returns GTM+5:30 and zzzz returns Indian Standard Time. This is my way
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSzzz"];
NSDate *date = [dateFormatter dateFromString:strDate];
You need to set the formatted locale to en_US_POSIX to force it to use the supplied timezone instead of the system timezone:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
You're parsing the date string correctly (in iOS 6 and later; this is not date format recognized in iOS 5 which recognizes -0400 or GMT-04:00, but not -04:00). Unfortunately, NSDate objects do not have a "time zone", so that information is not captured by the NSDateFormatter.
If you really want to capture the original timezone, I think you may have to manually parse the string for those last few characters (the -04:00), determine the timezone offset from that, and store this in a separate field and when outputting the date, use this separate time zone offset to set the timeZone property of your date formatter accordingly. Or if you really want to represent the original date, you could reformat this ISO8601 date string as human-friendly string and keep this pretty string (as well as the NSDate object, presumably).
BTW, splitting hairs, but you might want to be wary about assuming that -04:00 will represent EDT because (a) it depends upon the time of the year; and (b) there are other timezones that are also -04:00 (e.g. there are a bunch of South American timezones that are also -04:00).
Generally apps avoid this problem altogether by (a) converting the date strings to NSDate objects; and (b) output these NSDate objects using the timezone and locale of the device that the app is running on.

Objective-C - How to Get Date Formatter to Add Colon in Time Zone Offset

I'm trying to format a date to match the format expected on the server side.
Wanted: 1985-01-24T00:00:00-07:00
Got: 1985-01-24T00:00:00-0700
Using: yyyy-MM-dd'T'HH:mm:ssZZZ
Is there a date format trick I can use to get that colon in there?
Here is my code. _birthdate is the date supplied by the birthdate selector:
NSDate *birthdate = (NSDate *)resultObject;
[_birthdate setNewTitle:[IRDate mmddyyFromNSDate:birthdate]];
//Set server-ready birthdate format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
serverFormattedBirthDate = [formatter stringFromDate:birthdate];
NSLog(#"Birthdate: %#", serverFormattedBirthDate);
According to the Date Format Specifiers documentation, it looks like you'll need 5 Z's. That will get you things like "-08:00".
Aha, I see what you're getting at. If you run the formatting on OS X 10.8, you'll get the string you're expecting. However, if you run the formatting on iOS 5.1, you'll get the extra "GMT" in the string.
I'm guessing that the underlying data has changed in recent versions of the CLDR. In that case, I'm not sure what the correct answer is.

NSDateFormatter dateFromString produces nil after first call in same method

Before Xcode 4 I used to use NSDate initFromString but it is now deprecated and produces errors in Xcode 4.2. So I jumped over to using NSDateFormatter dateFromString but ran into an issue in a method I call that gets a sunrise date from string and a sunset date from string to determine if it is day or night (so same method). The first call works fine, but the second call returns nil. I decided to see if it was repeatable so I created the following simple method and dumped it into another project in an innocuous place:
- (void)testDateFormatter
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"yyyy-MM-dd hh:mm:ss Z"];
NSString *srDateTimeString = #"2011-11-09 07:08:00 -0800";
NSString *ssDateTimeString = #"2011-11-09 17:08:00 -0800";
NSDate *sunriseDateTime = [formatter dateFromString: srDateTimeString];
NSDate *sunsetDateTime = [formatter dateFromString: ssDateTimeString];
return;
}
The first call returns the correct date. The second call returns nil. I have tried several variations (such as creating two separate NSDateFormatters, preinitializing the dates, preinitializing the dates to the current time and then reassigning) but none do the expected thing.
Does anyone know what is going on here? Since I hadn't found any reference to this error anywhere I submitted it to Apple (bug #10420498).
Jack
In the second string, the hour is 17 (24-hour format) but the format string uses hh (lowercase) which is for 12-hour format.
Change the format string to yyyy-MM-dd HH:mm:ss Z.
In the documentation, see Date Formatters which contains a link to the Unicode Date Format Patterns listing each format specifier.

UIDatePicker and the day selection

trying to use a uidatepicker. have a method that fires when the value of the datepicker changes. it passes the date value, but when i try and take that date, my days portion is acting goofy. it returns the number of days since the beginning of the year, not the actual month day. so say February 3rd, instead of 3, i get 34.
-(IBAction)datePickerValueChanged: (id)sender;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD"];
self.date = [dateFormatter stringFromDate:[sender date]];
NSLog(#"date: %#", [dateFormatter stringFromDate:[sender date]]);
[dateFormatter release];
}
for april 12th, i get:
2011-10-27 14:22:41.939 Satshot[12789:40b] date02: 2011-04-102
i'm guessing my dateformatter is causing it, but i don't understand why.
You are using the wrong format specifier. You need "yyyy-MM-dd"
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
Update, YYYY and DD are both wrong. Thanks #jrturton.
From the Data Formatting Guide:
It uses yyyy to specify the year component. A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of "Week of Year"), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
In addition, DD should be dd.
Though why not just use the date to hold your value internally?
Also, note that creating a date formatter is an expensive exercise, valueChanged on a picker view could be called tens of times a second as the user scrolls through. If you must use a formatter, cache it.

Date/Time parsing in iOS: how to deal (or not deal) with timezones?

I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ which outputs 2011-04-05 16:28:22 -07:00. How do I parse that in iOS?
This is the code I'm messing around with right now:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:#"2011-04-05T16:28:22-0700"];
NSLog(#"%#; %#; %#", dateFormatter, date, [NSTimeZone localTimeZone]);
First thing to note is that 2011-04-05 16:28:22 -07:00 has to look like 2011-04-05T16:28:22-0700, where a T replaces the first space (assuming that stands for time, or where the time part of the string starts from?), the second space is removed and the colon in the time zone is removed. I figure I'll find a way to format the string that .NET is sending back to conform to the string iOS will parse.
The real issue is that the date that is outputted is 7 hours ahead of what I've sent in the JSON response. So, iOS outputs 2011-04-05 16:28:22 -07:00 as 2011-04-05 23:28:22 +0000, and that is wrong as far as my app is concerned.
The only solution I've found so far is to send the date in the JSON as 2011-04-05 16:28:22 +00:00, but that again is wrong because I'm altering what the real date should be.
Anyway, I'd appreciate someone taking a look and letting me know how I can parse the date string .NET is outputting via the format yyyy-MM-dd HH:mm:ss ZZZ (which I suppose can be re-written to yyyy-MM-ddTHH:mm:ssZZZ) to an NSDate object I can use in iOS.
I don't know how right this is, but I ultimately found that in .NET I have to do DateTime.ToUniversalTime().ToString("yyyy-MM-ddHH:mm:ss") and on the iOS side I have to do this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:#"2011-04-0600:28:27"];
Only then is the date correct when NSLog outputs, so I'm assuming that I've finally got a proper date/time.
Your iOS date parsing code is correct: 2011-04-05 16:28:22 -07:00 and 2011-04-05 23:28:22 +0000 represent the same time, just in different time zones. Your only problem is that NSDate doesn't actually store the time zone, and so [date description] outputs using UTC.
You're almost certainly better off just using UTC time everywhere for interchange and not bothering with time zones. I'm sure you can get that your ASP.NET code to do that, and it's easier to parse on the receiving end as well. NSDateFormatter uses standard Unicode date formatting syntax, which you can read about here.