daydreamer
daydreamer

Reputation: 92179

Objective-C: Could not parse the date from String

In my Transaction, I get the date in format as

"2014-11-16T23:18:17.880"

I try to parse the date as

NSDate *transactionDate = [self getTransactionDateFromString:transactionModel.date];

and

- (NSDate *)getTransactionDateFromString:(NSString *)dateString {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.Z'"];
    return [dateFormat dateFromString:dateString];
}

But this returns nil.

What am I doing wrong?

Upvotes: 0

Views: 84

Answers (1)

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

Your date value contains the millisecond in the end. So use SSS instead of using Z which will set your date according to the format.

Replace the below line :-

[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.Z'"];

To Modified line:-

[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"]; 

Upvotes: 1

Related Questions