How do I convert from NSString to NSDate using NSDateFormater - objective-c

I have this string: 2012-01-12T21:01:00 and this code:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate* arrivalTime=[df dateFromString:field_arrival_time];
But it returns nil. What date format should I use to parse this string?

Your string 2012-01-12T21:01:00 contains the literal T (At least I believe it's a literal, it doesn't appear to signify a timezone). You must include this in your date format.
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss"];
Note the lack of the a in the date format, using it will require your input string to use AM or PM preceded by a white space. For more information on special characters with NSDateFormatter take a look at the Date Formatting Guide paying extra attention to the Fixed Formats.
Edit: Your input string does not specify a timezone, it will probably be interpreted as UTC and be localized to the timezone of your machine when you output it through NSLog().

I have tried a lot on your string but never get result. only nil shows on console. but when i remove character "T" from your string it gives perfect result.
[f setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:#"2012-01-12 21:01:00"];
NSLog(#"date %#", date);
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-yyyy"];
NSString *s = [f2 stringFromDate:date];
NSLog(#"S %#", s);
/// OutPut : S 12-01-2012

Related

NSDateFormatter returns nil

I'm trying to parse a date passed in the format:
"2014-03-26T05:07:42.14286Z"
My NSDateFormatter code looks like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
self.createdAt = [dateFormatter dateFromString:#""2014-03-26T05:07:42.14286Z""];
But it just returns nil. I've tried multiple variations with and without the ticks but I seem to be missing something. Am I using NSDateFormatter incorrectly, misunderstanding the usage of ticks or something else entirely?
The formatter returns nil if the given string doesn't correspond to the expected format. Your format string was almost right, you just needed to :
Apostrophe should only be used for literals
Milliseconds should be specified as 'SSS' and match the separation (use ss.SSS)
The correct format string is :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *d = [df dateFromString:#"2014-03-26T05:07:42.14286Z"];
Just replace the below line to modified line
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'SS'Z'"];
Modified line:-
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];

iOS create NSDate from output of Python datetime.isoformat()?

My python backend uses the isoformat() method on UTC date times, which results in strings that look like 2014-01-14T18:07:09.037000. Following other examples, I'm trying to create NSDates from those strings (passed up in JSON packets):
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:S"];
NSLog(#"cycle_base %#", myFields[#"cycle_base"]);
self.cycleBase = [dateFormatter dateFromString: myFields[#"cycle_base"]];
NSLog(#"cycleBase %#", self.cycleBase);
I've tried variants on the S part (which is supposed to be fractional seconds?) of the format string, but to no avail. I always get a nil back. What am I doing wrong?
iOS 7 follows the Unicode Technical Standard #35, which is a list of format patterns.
In this document you will find that the format string for fractional seconds is capitalized S.
NSString *string = #"2014-01-14T18:07:09.037000";
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.S";
NSDate *date = [formatter dateFromString:string];
NSLog(#"%#", date);
This will net you a valid NSDate object. Don't forget to set the proper time zone and locale on your NSDateFormatter object.

Date with month name date, year in objective-c

I have a date string formatted as "September 10, 2013". How can I convert this representation into a format such as "yyyy/mm/dd".
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df dateFromString:strDate];
NSDate *date = [df dateFromString:strDate];
[df setDateFormat:#"yyyy/mm/dd"];
NSString* temp = [[NSString alloc] init];
temp = [df stringFromDate:date];
NSLog(#"date i required %#", temp);
temp object is null here.
Thanks in advance.
Let's rework your original code, and discuss it as well:
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
For the input you're attempting to parse, you've got to provide a format string for your date formatter. If you don't, the date formatter is going to use defaults that are determined by the settings in your Date & Time control panel. Not only do these settings vary from locale to locale, they're subject to whatever changes the user may have effected. Another way of putting it is that they might not match the format of the date string you're attempting to convert.
You also don't want to rely on predefined styles such as NSDateFormatterShortStyle, NSDateFormatterMediumStyle or NSDateFormatterLongStyle. They're meant for date display (if you're willing to accept the Date & Time control panel settings), not for parsing.
There's a document you should consult, which is Unicode Technical Standard #35. Look in the table labeled "Date Field Symbol Table." Based on the information presented there (and your input), you'd set up your date converter to parse with a format string like this one:
[df setDateFormat:#"MMMM dd, yyyy"];
Now you can use your date formatter to parse your date string, and it'll work:
NSDate *date = [df dateFromString:strDate];
From this point onward, you're looking pretty good (though I've removed a superfluous line or two throughout all of this):
[df setDateFormat:#"yyyy/MM/dd"];
NSString *temp = [df stringFromDate:date];
NSLog(#"date i required %#", temp);
Best wishes to you in your endeavors.
You can use the NSDateFormatterclass -- look at the stringFromDate and dateFromString methods.
UPDATE: you have a couple of problems -- first, you need to tell the formatter what the initial format should be. Second, you have a format problem with the second format string -- 'm' is minutes, 'M' is months. You should review the documentation here. Here is an example:
NSString *strDate = #"September 10, 2013";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterLongStyle];
[df dateFromString:strDate];
NSDate *dat = [df dateFromString:strDate];
[df setDateFormat:#"yyyy/MM/dd"];
NSString* t = [df stringFromDate:dat];
NSLog(#"date i required %#", t);

Problems with Creating an NSDate object from a ISO 8601 string

I'm having problems converting a string into a NSDate object and I'm not sure why.
NSString* tester= #"2012-11-26T10:20:40.187";
NSLog(#"", tester); //Print the Date String
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [formatter dateFromString:tester]; //Store the date
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
NSLog(#"DATE OBJECT:%#\nDATE STRING: \"%#\"", [output stringFromDate:date]); //Print the NSDate object and the string
But all I seem to get is:
DATE OBJECT:(null)
DATE STRING: "2012-11-26T10:20:40.187"
I figure it has something to do with the .187 but I'm not sure. I'm sorry if this is a duplicate but I couldn't figure this out.
Thank you very much in advance!
The abbrevation for milliseconds is "S"not Z which is TimeZone.
It seems that you have read the correct document where that example is from, but
you missed the link to the Unicode Technical Standard #35.
See NsDateFormatter Docu, search for "Formatters in Mac OS X v10.4 use version tr35-4."
try that below:
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
See also NSDateFormatter Question

Convert Short Zulu format date to NSDate

I am having troubles converting a short Zulu date format to a NSDate Object. I have found some answers for converting Zulu strings but mine looks like:
20111210T1000
And based on my researches, I am trying to do:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyyMMdd'T'HHmmss Z"];
NSDate *date = [f dateFromString:[str stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
[f release];
I've tried many ways but my date is still nil...
How should I set my NSDateFormatter?
Here is a quick fix, in your date string you miss the timezone in the format, you should append (Paris one here) to your string and it should work. Also the format was wrong.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd'T'SSSZ"];
NSString* str = #"20111210T1000-0100"; // NOTE -0100, GMT +1 Paris zone
NSDate* date = [df dateFromString:str];
NSLog(#"%#", date);