danchez
danchez

Reputation: 1446

How Do You Figure Out What the Date Format Is For a Given NSString?

Hey StackOverflow People,

I have a date timestamp NSString with the following value that I get from a backend server:

NSString *date = @"2014-01-27T21:06:59.000-05:00";

I can't for the life of me figure out what the date format is for the string above and have been trying to use a NSDateFormatter object with the two following date formats (that have both returned me nil):

Format 1: @"yyyy-MM-ddThh:mm:ss.000-05:00";

Format 2: @"yyyy-MM-ddThh:mm:ss.SSS-SS:SS";

My question is: how the heck do you use an already given string to determine what date format you're working with?

Upvotes: 0

Views: 292

Answers (1)

Antonio MG
Antonio MG

Reputation: 20410

Try this, it works:

@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

The -05:00 is the Timezone, you need to use ZZZZZ for that.

Also, because T is not part of the date, you need to scape it, use 'T' for that.

Notice the HH instead of hh.

Find all the formatters with examples here:

http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

About your question of how to know the format, you have to guess it based on what you see, or ask the person providing that date.

Upvotes: 3

Related Questions