Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse ISO 8601 using NSDateFormatter with optional milliseconds part

I'm trying to use an NSDateFormatter to parse dates that are in either of these formats

@"2013-02-01T14:21:00"

or

@"2013-02-01T14:21:56.345"

Currently I am using the below method to parse the string and return a date:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [dateFormatter dateFromString:dateToFormat];

This works fine for the first style of date but returns nil for a string that includes the milliseconds part.

I suppose I could test for the existence of the milliseconds and strip them but I was wondering if I could change the date format to treat the .SSS as optional?

Thanks for your help

like image 868
Dan Rowlands Avatar asked Feb 14 '13 14:02

Dan Rowlands


2 Answers

As far as I know there is no way to make optional parameters.

The usual solution is to use two formatters, one for each format. To decide which formatter to use, you can either

  1. Count the number of characters in the date string (as suggested in Parsing a RFC 822 date with NSDateFormatter)

  2. Just try both formatters and get the first non-nil result.

Since your date formats are similar, you can go with only one formatter and if the date string is too short, append .000 before using the formatter.

like image 106
Sulthan Avatar answered Nov 18 '22 20:11

Sulthan


The correct approach since iOS 10 is to use ISO8601DateFormatter specifically created to handle all variations of ISO 8601 date strings. Please see the example below:

let date = Date()
var string: String

let formatter = ISO8601DateFormatter()
string = formatter.string(from: date)

let GMT = TimeZone(abbreviation: "GMT")
let options: ISO8601DateFormatOptions = [.withInternetDateTime, .withDashSeparatorInDate, .withColonSeparatorInTime, .withTimeZone]
string = ISO8601DateFormatter.string(from: date, timeZone: GMT, formatOptions: options)

And Objective-C version:

NSDate *date = [NSDate date];
NSString *string;

NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
string = [formatter stringFromDate:date];

NSTimeZone *GMT = [NSTimeZone timeZoneWithAbbreviation: @"GMT"];
NSISO8601DateFormatOptions options = NSISO8601DateFormatWithInternetDateTime | NSISO8601DateFormatWithDashSeparatorInDate | NSISO8601DateFormatWithColonSeparatorInTime | NSISO8601DateFormatWithTimeZone;
string = [NSISO8601DateFormatter stringFromDate:date timeZone:GMT formatOptions:options];
like image 1
Vadim Bulavin Avatar answered Nov 18 '22 21:11

Vadim Bulavin