Bhumeshwer katre
Bhumeshwer katre

Reputation: 4671

NSDateFormatter difference

What is the difference between these two Date Formats. First one give actual time but second on give time buy adding time zone offset value.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:@"2013-12-02T12:15:43.182Z"];
NSLog(@"Date: %@",dateConverted); // 

Date: 2013-12-02 12:15:43 +0000

NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter1 setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *dateConverted1 = [dateFormatter1 dateFromString:@"2013-12-02T12:15:43.182Z"];
NSLog(@"%@",dateConverted1);

Date: 2013-12-02 06:45:43 +0000

Upvotes: 0

Views: 135

Answers (2)

zaph
zaph

Reputation: 112855

The second date formatter is incorrect, the 'Z' should not be single quoted, that keeps it from being considered a format character.

Also the only single quotes that are needed are around the 'T' so that is is not considered a format character but rather a literal.

See ICU User Guide: Formatting Dates and Times

Upvotes: 2

rmaddy
rmaddy

Reputation: 318854

The problem with the 2nd format is all of the needless quotes, especially around the Z. By quoting the Z this means the Z is treated as a literal character and not the timezone format specifier.

Get rid of the quotes around the Z and both will give the same result.

Upvotes: 3

Related Questions