0

I'm trying to separate the month and day from the following string.

2012-06-29

I've tried formatting the string into the medium style by doing the following:

//Setting Up My Formatter
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];

Forming String Into Formatted Date
NSDate *dateFromString = [formatter dateFromString:event.date];

//Separating Components
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:dateFromString];

//Assigning Each Component to my Cell's Labels
cell.dayLabel.text = [NSString stringWithFormat:@"%d", [components day]];
cell.monthLabel.text = [NSString stringWithFormat:@"%d", [components month]];

When I ran the NSLogs my "event.date" came back "2012-06-26 & dateFromString came back null. Can anyone help guide my in the right direction? Thanks a lot!

EDIT:

So I finally figured out from your help that I was setting the format incorrectly. Instead of using:

[formatter setDateStyle:NSDateFormatterMediumStyle];

I am now using:

[formatter setDateFormat:@"yyyy-MM-dd"];

Here is my question though, from the components, the month is coming back as an integer (1, 2, 3) but I want it to come back as "MMM" (Jan, Feb, Mar). Thanks!

Community
  • 1
  • 1
Year3000
  • 459
  • 2
  • 7
  • 15
  • See http://stackoverflow.com/questions/4258266/nsdateformatter-datefromstring-returns-nil You might need to set the Locale of your date formatter. – Dima May 16 '12 at 14:00

3 Answers3

2

Here's how I would do it:

First create a NSDateFormatter to take in your string properly:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:"yyyy-MM-dd"];

Get a date from your string:

NSDate *yourDate = [formatter dateFromString:@"2012-06-29"];

Get the component for your date:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:yourDate];

Get the properties of the component:

[components day];    
[components month];
[components year];

Release what needs to be released:

[formatter release];

Comment reply:

Yes, you should pretty much just do the reverse.

Create another NSDateFormatter (using reference:http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns)

NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
[monthFormat setDateFormat:@"MMM"];

NSString *month = [monthFormat stringFromDate:yourDate];

[monthFormat release];

` You can at this point truncate/capitalize it to whatever you want, as you'll get it in lowercase, and perhaps a bit too long. (check the ref)

Root Fool
  • 324
  • 2
  • 11
  • Thanks but for the month I actually want the format as APR, JUN, or OCT... instead of 4, 6 or 10. Any chance this is possible? – Year3000 May 16 '12 at 14:10
1

The dateFromString is failing because you have told it to expect a string in the following format (from the NSDateFormatter reference):

Nov 23, 1937 3:30pm

You need to set a custom format using something like this:

[formatter setDateFormat:@"yyyy-MM-dd"];
Jon Grant
  • 11,369
  • 2
  • 37
  • 58
  • Thanks but for the month I actually want the format as APR, JUN, or OCT... instead of 4, 6 or 10. Any chance this is possible? – Year3000 May 16 '12 at 14:15
0

Try to set setDateFormat

[formatter setDateFormat:@"yyyy-MM-dd"];

Rohit Dhawan
  • 1,163
  • 1
  • 11
  • 23