Ravi Ojha
Ravi Ojha

Reputation: 1480

Nsdateformatter with nsdate for getting exact date in iOS

I want to get the date from xml data ,which is in a string like as given below "Fri, 06 Feb 2015 11:25:00 -0600" how can i set the nsdateformatter for this type of date " -0600 is the time zone". Please help me

Upvotes: 0

Views: 418

Answers (1)

zaph
zaph

Reputation: 112873

See: ICU Formatting Dates and Times
Also: Date Field SymbolTable., look specifically at "Z".

Z Time Zone: ISO8601 basic hms? / RFC 822 -0800

Example:

NSString *dateString = @"Fri, 06 Feb 2015 11:25:00 -0600";
NSLog(@"dateString: %@", dateString);

Input dateString:

dateString: Fri, 06 Feb 2015 11:25:00 -0600

NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date: %@", date);

Output (in timezone GMT):

date: 2015-02-06 17:25:00 +0000

To create a string from the date in a specific format:

NSDateFormatter *displayFormatter = [NSDateFormatter new];
[displayFormatter setDateFormat:@"E, dd MMM yyyy H:mm:ss Z"];
[displayFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]];
NSString *displayString = [displayFormatter stringFromDate:date];

Output (in timezone CST):

displayString: Fri, 06 Feb 2015 11:25:00 -0600

Upvotes: 2

Related Questions