How to turn a NSString into NSDate?
Solution 1
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.
Solution 2
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"]];
Solution 3
Did you try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];
Cheers
jennas
I'm a Full-Stack developer with over 8 years professional experience in both front and back end application development. I have a particular interest in following good practice such as using design patterns, dependency injection, responsive design and modern Javascript techniques.
Updated on November 05, 2020Comments
-
jennas over 3 years
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?
-
jennas over 13 yearsHi, yes i have this NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *dateT = [dateFormatter dateFromString:@"2011-01-13T17:00:00+11:00"]; NSLog(@"date From string: %@", dateT); This returns (null)
-
Aditya over 13 yearsThis will help you stackoverflow.com/questions/4655199/…
-
jennas over 13 yearsHi, im setting [NSTimeZone systemTimeZone] which is the correct zone. The following still returns null bloody thing! [dateFormatter setDateFormat:@"yyyy-MM-dd'T'hh:mm:ssSSS"];
-
tbone over 13 yearsThat was for my code, you'll need to do something similar for your code. Again, check the UNICODE docs.
-
jennas over 13 yearsThank you this is perfect. The timezone in single quotes was not expected but was what fixed it :)
-
Nick N about 10 yearsAlso, mostly search now for utilities on cocoapods. Here's the query on NSDate. cocoapods.org/?q=NSDate
-
user1244109 almost 7 yearscouldn't figure out that 'T' in the middle. So that helped. Thanks!