0

(this applies to OSX, have not tested on iOS)

Before you ask 'why such a silly date format': I use meta data pulled from files created on the iPhone/iPad. Photos and videos contain a 'creationDate' Metadata Tag that returns an NSString in exactly this format. Now if I feed this string:

@"2014-11-21T08:05:16+0400"

into NSDateDetector the following way:

__block NSDate *detectedDate;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:nil];
[detector enumerateMatchesInString:self
                           options:kNilOptions
                             range:NSMakeRange(0, [self length])
                        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)

 { detectedDate = result.date; }];

 NSLog(detectedDate.description);

I receive a completely bogus value:

"2014-12-12 14:16:00 CET"

  • The date "2014-12-12" is today's date, not 2014-11-21 as it should
  • The time "14:16:00 CET" is complete fracked, not even the time the code ran

So we have at least two possibilities here - basically NSDateDetector can't process the standard format Apple encodes dates on the iPhone OR I've made an obvious error in my code that I can't find. I'd be happy if the second was true, so can you tell me what I'm doing wrong?

Oh, I'm running this on Mavericks / XCode 5. Anyone? Please? -ch

cfx Chris
  • 51
  • 6
  • Also never do something like that `NSLog(detectedDate.description)`. Write `NSLog(@"%@", detectedDate)` instead as the first argument of NSLog is a format string. – Julien Dec 12 '14 at 10:35
  • That's not a "silly date format", that's pretty much straight [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Which might help you [find the right answer...](http://stackoverflow.com/questions/4999396/how-to-parse-a-date-string-into-an-nsdate-object-in-ios) – Matt Gibson Dec 12 '14 at 10:40
  • Indeed, Matt, thank you - which makes it so strange that NSDataDetector can't correctly parse it. The reason I would like to use NSDataDetector is that I feed different date formats from different sources into the same routine, and would love to have a one-stop approach. NSDataDetector seemed to be the logical approach. – cfx Chris Dec 12 '14 at 10:47

1 Answers1

2

NSDataDetector is not meant to parse dates like that. Data detectors are meant to detect "human" patterns in written text.

If you want to parse dates, you should use NSDateFormatter.

Julien
  • 3,427
  • 20
  • 22
  • Julien, thank you for the hint. The issue I'm trying to fix is that the code receives different date strings from different sources that may not all be formatted the same way. I thought that using NSDataDetector would allow me to parse multiple date formats without prior knowledge of the exact format. It does work nicely with many different date formats - except the ISO Standard, which I ascribe to an oversight on my side. – cfx Chris Dec 12 '14 at 11:17
  • 1
    I concur. Data Detectors ignores machine-generated dates on purpose to avoid visual pollution with links (because it's unlikely the user will create an event from this kind of text) – Thomas Deniau Dec 12 '14 at 13:02