Problems with Creating an NSDate object from a ISO 8601 string - objective-c

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

Related

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.

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);

How can I Convert a NSString to a valid NSDate (iOS)

I'm passing a string from javascript to Objective-C in the form of "2012-02-17 14:21:30 +0000".
My code is as follows:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDate *notificationDate = [NSDate dateWithString:firingDate];
The issue is that I ended up reading the OS X reference instead of the iOS docs (doh!) so this throws a warning as dateWithString isn't present in iOS. In theory I suppose that this shouldn't work at all but it does, albeit with that warning.
What is the Correct way to convert the string to a NSDate?
The correct way is to use NSDateFormatter as a factory to create dates from strings (and vice versa).
Try:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"YYYY-MM-dd HH:mm:ss Z";
NSDate *notificationDate = [formatter dateFromString:firingDate];
Try this:
NSString *firingDate = [_parameters objectForKey:#"fire"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *notificationDate = [dateFormatter dateFromString:firingDate];
Check out the reference for parsing dates from multiple regions.
Don't forget to release your formatter when finished.

How do I convert from NSString to NSDate using NSDateFormater

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

How to turn a NSString into NSDate?

Ive been racking my brains with no luck. Could someone please tell me how i would convert this string:
"2011-01-13T17:00:00+11:00"
into a NSDate?
The unicode date format doc is here
Also, for your situation, you could try this:
// original string
NSString *str = [NSString stringWithFormat:#"2011-01-13T17:00:00+11:00"];
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(#"Date: %#", dte);
// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:#"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(#"DateString: %#", dateString);
[dateFormat release];
[dateFormat2 release];
Hope this helps.
put the T part in single quotes, and check the unicode docs for the exact formatting. In my case, I have something similar, which I do this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss.SSS"];
Again, not exactly the same, but you get the idea. Also, be careful of the timezones when converting back and forth between strings and nsdates.
Again, in my case, I use:
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
Did you try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];
Cheers
You might check out TouchTime.
https://github.com/jheising/TouchTime.
It's a direct port of the awesome strtotime function in PHP in 5.4 for Cocoa and iOS. It will take in pretty much any arbitrary format of date or time string and convert it to an NSDate.
Hope it works, and enjoy!
Try using this cocoapods enabled project. There are many added functions that will probably be needed as well.
"A category to extend Cocoa's NSDate class with some convenience functions."
https://github.com/billymeltdown/nsdate-helper
Here's an example from their page:
NSDate *date = [NSDate dateFromString:#"2009-03-01 12:15:23"];