NSDate not pulling date from string - objective-c

I have a String which looks like this
NSString *string = #"2012-04-30T23:59:00+10:00";
Right now, I am trying to convert that into NSDate format (So I can store the data in a database at a later stage). Right now, I can't seem to get a date format that works with my current date. I am using
NSDateFormatter *assignmentDateFormat = [[NSDateFormatter alloc] init];
[assignmentDateFormat setDateFormat:#""];
to parse my date but I just cant seem to get the combination right (e.g. YYYY-MM-dd etc.).
Any help would be greatly appreciated and if someone knows how to do the timezone part (+10:00) that would be amazing. Thanks!

The full specification is here: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
(Kinda tricky to find in Apple documentation... bookmark it!)
Edit: OK, try this one: yyyy-MM-dd'T'HH:mm:ssZ

There are a simple tutorial here: http://www.picksourcecode.com/ps/ct/161110.php

Use this:
NSDateFormatter *assignmentDateFormat = [[NSDateFormatter alloc] init];
[assignmentDateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss+10:00"];

Related

How to to take time as input OS X

I am developing an application for OS-X. I want to take time as input from user. I have placed three text fields like below
But If I take input in this way How am I going to convert it into date&Time ? Because I want to compare this with system time.
How can I do that please advice ?
Concate all UITextField data i.e
NSString * strTime = [NSString stringWithFormat:#"%#:%# %#",txtHours.text, txtMin.text, txtAMPM.text];
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"HH:mm a"];
NSDate *startD = [dateFormatter1 dateFromString:strTime]; // this code is used for converting string into nsdate
Now, you can compare current system time with this above NSDate.
Hope this will help you :)

Opposition of localizedStringFromDate: dateStyle: timeStyle:

When I format a date via:
[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]];
save it somewhere, then try to access it, i get a NSString. How to get it as a NSDate?
BTW: I want a method where i put NSDateFormatterStyle as argument. Otherwise it will be wrong- in different locale it will be saved as different string, so formatting it as
[dateFormat setDateFormat:#"yy/MM/dd"];
or any other options of this kind - will create an error. Or at least i think so ;).
Thanks for any responses.
NSDateFormatter returns a NSString representation of the NSDate object. NSDate is format insensitive, meaning it isn't tied to some locale, it's actually stored as a number. You use the NSDateFormatter to present that numerical value as a localized representation.
To convert a NSString back to an NSDate you can use something like this:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yy/MM/dd"];
NSDate *myDate = [df dateFromString: dateString];
It sounds like you want to store the NSDate in a way that can be perfectly parsed both ways. In that case I'd recommend storing it as an ISO8601 date, and you'll need to use an nsdateformatter more custom-like to do that. There are plenty of libraries/categories out there on github or stackoverflow to help you.

NSDates changed in UITableView Header

So I am using an array of dates as Section Headers in my UITableView. The dates of my data in the arrays is as follows
07/12/2012
07/13/2012
07/14/2012
But when I run the app the section headers are all moved back one day so they are as follows:
07/11/2012
07/12/2012
07/13/2012
What gives? The data I am pulling from the server is specific to timeZone. We know when our app will be used and on what day (think traveling circus).
I am sure this has to do with NSTimeZone, so I tried the following, which did not work.
self.sectionDateFormatter = [[NSDateFormatter alloc] init];
[self.sectionDateFormatter setDateStyle:NSDateFormatterLongStyle];
[self.sectionDateFormatter setTimeStyle:NSDateFormatterNoStyle];
[self.sectionDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
Anyone know have a solution?
Set the dates to UTC and it fixed the issue.

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.

Is there a way to convert a natural language date NSString to an NSDate

Say I have the NSString #"tomorrow"
Is there any library that takes strings such as this and converts them into NSDates? I'm imagining/hoping for something like this:
NSString* humanDate = #"tomorrow at 4:15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"x at HH:MM"];
NSDate* date = [dateFormatter dateFromString:humanDate];
I would also want to do things like "Next monday", etc. but it doesn't have to be super sophisticated. I can enforce rules on input, but I'd like a little bit of natural language available.
My alternative is to take the string, break it up into pieces, and format it manually. But I was hoping someone has already done this.
Do you mean something like dateWithNaturalLanguageString:?
From the link:
dateWithNaturalLanguageString:
Creates and returns an NSDate object set to the date and time specified by a given string.
+ (id)dateWithNaturalLanguageString:(NSString *)string
A string that contains a colloquial specification of a date, such as “last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,” or “31/12/01.”