5

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
  • 2,444
  • 2
  • 25
  • 31

5 Answers5

20

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.

tbone
  • 15,107
  • 3
  • 33
  • 40
3

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"]];
tbone
  • 15,107
  • 3
  • 33
  • 40
  • Hi, 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"]; – jennas Jan 13 '11 at 13:30
  • That was for my code, you'll need to do something similar for your code. Again, check the UNICODE docs. – tbone Jan 13 '11 at 14:54
1

Did you try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];

Cheers

Aditya
  • 4,414
  • 5
  • 29
  • 40
  • Hi, 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) – jennas Jan 13 '11 at 12:58
  • This will help you http://stackoverflow.com/questions/4655199/how-to-set-date-format-retrieved-from-uidatepicker/4655297#4655297 – Aditya Jan 13 '11 at 13:01
0

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!

Jim Heising
  • 4,260
  • 2
  • 16
  • 16
0

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"];
Nick N
  • 984
  • 9
  • 22
  • Also, mostly search now for utilities on cocoapods. Here's the query on NSDate. http://cocoapods.org/?q=NSDate – Nick N Apr 16 '14 at 03:18