Question

We are currently creating an interface that will consume existing XML and have ran into an issue with parsing the date response. The XML is going to be consumed in an IOS Application.

The DateTime format of the XML is: MM/dd/yyyy HH:mm:ss.fffffff

As such, we need to convert that response into an appropriate NSDate. However, we have not been able to get that format to work.

A simple test:

NSDateFormatter * df = [[NSDateFormatter alloc] init];  
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss.fffffff"];  
NSDate * dob1 = [[NSDate alloc]init];  
dob1 = [df dateFromString:@"03/26/1983 01:10:10.0000000"];

... dob1 will return nil

NSDateFormatter * df = [[NSDateFormatter alloc] init];  
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss.0000000"];  
NSDate * dob1 = [[NSDate alloc]init];  
dob1 = [df dateFromString:@"03/26/1983 01:10:10.0000000"];

... This does work, but only allows for '.0000000'

Any suggestions would be appreciated. Thanks.

Était-ce utile?

La solution

you should use this formatter

@"MM/dd/yyyy HH:mm:ss.SSSSSSS"

Autres conseils

To expand on rmaddy's answer, the values you can put in the format string for NSDateFormatter depend on the version of iOS (or OS X) you're targeting.

From the Data Formatting Guide:

Fixed Formats

To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system:

  • Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.

  • Formatters in iOS 5.0-5.1 use version tr35-19.

  • Formatters in OS X v10.7 and iOS 4.3 use version tr35-17.

  • Formatters in iOS 4.0-4.2 use version tr35-15.

  • Formatters in iOS 3.2 use version tr35-12.

  • Formatters in OS X v10.6 and iOS 3.0-3.1 use version tr35-10.

You should always double-check that the formatters you want to use are going to be available on all platforms that you support.

In your case, you're looking for "Fractional Second", which rmaddy correctly points out is S, on both iOS 5.x and iOS 6.x

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top