9

I tried to get a NSDate from a NSString with UNKNOWN format, So I wrote a function like below

-(void)dateFromString:(NSString*)string {

    NSError *error = NULL;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];

    NSArray *matches = [detector matchesInString:string
                                         options:0
                                           range:NSMakeRange(0, [string length])];

    NSLocale* currentLoc = [NSLocale currentLocale];
    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypeDate) {
            NSLog(@"Date : %@", [[match date] descriptionWithLocale:currentLoc]);
        }
    }
}

It works well except for one place.

If I invoke

[self dateFromString:@"6/12"];

It prints

Date : Thursday, June 12, 2014 at 12:00:00 PM Australian Eastern Standard Time

At the same time if I call

[self dateFromString:@"13/12"];

it prints

Date : Friday, December 13, 2013 at 12:00:00 PM Australian Eastern Daylight Time

Basically, I want the function to behave consistent. Since I live in Australia, it should have returned December 6 for the first execution. Second call result is correct.

What am I doing wrong here?

Bavan
  • 971
  • 1
  • 8
  • 22
  • Why don't you use [NSDateFormatter](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html), it's flexible, and you can solve your problem with that easily, as you can set the format of your date string. You set your date format with setDateFormat, and then you call dateFromString: to convert string to date – mrt Dec 06 '13 at 08:08
  • 2
    I think there is not a good way to handle UNKNOWN format date string. You should determine the format of date string – Joiningss Dec 06 '13 at 08:29
  • Incredible, how my google search got me right to what i was looking for, ty mate:) – petert0th Jan 06 '16 at 14:39

2 Answers2

5

Actually, the method I wrote works really well :). Unfortunately, the Region Format in my test phone was set to US and was never set back to Australia : My bad..

@joiningss : Throw some random formatted date string to that methods and you will be amazed, how apple has made it easy for developers. Anyway, thank you so much.

@mrt,Chavda&Greg : Thanks a lot guys. I really appreciate your help.

Bavan
  • 971
  • 1
  • 8
  • 22
0

You have to use NSDateFormatter. Try replace your if statement with:

if ([match resultType] == NSTextCheckingTypeDate) {
   NSDate *dateAfter = [match date];
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
   NSString *formattedDateString = [dateFormatter stringFromDate:dateAfter];
   NSLog(@"After: %@", formattedDateString);
}

If you want to display it in different format you have to change this line to required format:

[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

If you want it to match your first example change it to:

[dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy 'at' HH:mm:ss a zzzz"];
Pradhyuman sinh
  • 3,938
  • 1
  • 19
  • 38
Greg
  • 24,353
  • 5
  • 47
  • 60