2

I've been having some issues converting strings to an NSDate, so maybe there's a better/easier way to do this.

I have an NSString of a date formatted like "dd/mm/yyyy", HOWEVER, some do not contain the year and are just "dd/mm".

I'm trying to store the day and month in my SQLite database, so I need to get those components out separately. I have tried converting them into NSDate but because some don't have years, it doesn't match the NSDate format and fails. I've also had issues following examples like this because my original dateString is not in the same format (uses slashes rather than dashes).

Any ideas? Thanks

Community
  • 1
  • 1
user339946
  • 5,961
  • 9
  • 52
  • 97

6 Answers6

3
NSString *dateString = @"01/04/1981";
NSArray *dateComponentsArray = [dateString componentsSeparatedByString:@"/"];
NSString *monthString = [dateComponentArray objectAtIndex:0];
NSString *dayString = [dateComponentsArray objectAtIndex:1];
if ([dateComponentsArray objectAtIndex:2]) {
    NSString *yearString = [dateComponentsArray objectAtIndex:2];
}

NSFormatter seems like overkill as it's pretty expensive. If you're sure you're not going to have any changes to the format, this should work.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
SushiGrass Jacob
  • 19,425
  • 1
  • 25
  • 39
2

SQLite doesn't even have a proper native date type (see my SO recent post on this topic), and to the extent you can store dates in SQLite, it doesn't make sense in the absence of a year. Not sure it makes sense to have a NSDate without a year, either.

Personally, I'd just store three fields, day, month, and year as separate numeric fields in the database, leaving the year blank if you don't know what year the person was born. Thus, just use a simple NSScanner as suggested by @benzado:

int day;
int month;
int year;

NSScanner *scanner = [NSScanner scannerWithString:birthdayString];

if ([scanner scanInt:&month] &&
    [scanner scanString:@"/" intoString:nil] &&
    [scanner scanInt:&day])
{
    // found valid month/day

    if ([scanner scanString:@"/" intoString:nil] && 
        [scanner scanInt:&year])
    {
        // found year too
    }
}

Or use NSString's componentsSeparatedByString to get it into an array of strings as suggested by @Jacob:

NSString *monthString, *dayString, *yearString;

NSArray *dateComponents = [birthdayString componentsSeparatedByString:@"/"];

if ([dateComponents count] >= 2)
{
    monthString = [dateComponents objectAtIndex:0];
    dayString   = [dateComponents objectAtIndex:1];

    // found month and day

    if ([dates count] == 3)
    {
        yearString = [dateComponents objectAtIndex:2];

        // found year, too
    }
}
Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • The question is about parsing the string representation, not how to store the date. – benzado Apr 28 '12 at 00:19
  • I had seen so many discussions about what you can do with NSDates, that I thought everyone was fixated on that. But you're probably right, that @user339946 was just wondering how to get the day, month, and year from a string in the format of either "mm/dd" or "mm/dd/yyyy". I've modified my answer accordingly, though you successfully already answered it. – Rob Apr 28 '12 at 02:01
2

You could use good ol' fashioned NSScanner.

NSScanner *scanner = [NSScanner scannerWithString:x];
int month, day;
if (
    [scanner scanInt:&month] &&
    [scanner scanString:@"/" intoString:nil] &&
    [scanner scanInt:&day]
) {
    NSLog(@"Got month = %d and day = %d", month, day);
}

Check this against the documentation because I'm writing from memory.

benzado
  • 82,288
  • 22
  • 110
  • 138
1

You need to figure out what it means when there's no year on the date. Does it assume the current year, or something else? Once you get that figured out, you can append the year to the string and then parse the date.

Eric Andres
  • 3,417
  • 2
  • 24
  • 40
  • Sorry, let me clarify. I'm grabbing birthdays from a user's Facebook friend list. Some user's do not include birthday, some do not include year, otherwise you get the string returned as "13/2/1984". I don't actually need the year, I just need to the month and date so I can store them in my database. – user339946 Apr 27 '12 at 21:12
  • If you don't care about the year, just the anniversary, then put the current year on it, and keep in mind in the future that you can't count on the year to be the actual birth year. – Eric Andres Apr 27 '12 at 21:19
  • Don't just use current year: if the date is Feb 29 and it's not a leap year, you'll run into problems. – benzado Apr 28 '12 at 00:14
  • Good point. Maybe just store day and month in different fields. – Eric Andres Apr 28 '12 at 02:55
1

can't you just check for '/' if its that simple? try to use NSDateFormatter as well, that may help you.

TommyG
  • 4,145
  • 10
  • 42
  • 66
  • I tried to use NSDateFormatter, but it doesn't seem to correctly convert. A string of "12/25/1987" somehow turns into an NSDate of 1986-12-21 – user339946 Apr 27 '12 at 21:19
1

I think this has the answers you need:

Apple iOS Date Formatting guide

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41