IOS how to set date format

I have a line like this

NSString *datestr = @"2013-08-06T03:51:54+00:00"; 

I am trying to set dateformat as below:

 NSDateFormatter *dformat = [[NSDateFormatter alloc]init]; [dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDate *date = [dformat dateFromString:datestr]; 

But the date will be zero, so can anyone tell me how to set the dateformat, thanks!

+8
ios nsdate
source share
3 answers

Try the following:

  NSDateFormatter *dformat = [[NSDateFormatter alloc]init]; [dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; NSDate *date = [dformat dateFromString:datestr]; 

Add ZZZ to the end of your formatting.

+12
source share

Try this code

 NSString *datestr = @"2013-08-06T03:51:54+00:00"; NSDateFormatter *dformat = [[NSDateFormatter alloc]init]; [dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"]; NSDate *date = [dformat dateFromString:datestr]; 

use small z where z gives you - with a specific non-location format. Where this is not possible, returns to the localized GMT format. Use one to three letters for the short format or four for the full format. In short format, metazone names are not used if the commonUsed flag is not specified in the locale

for the zone for more information on date format patterns in lens c see this link http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns hope this helps you

+6
source share

try below:

 NSString *dateString = @"2013-08-06T03:51:54+00:00"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; NSDate *date; NSError *error; [formatter getObjectValue:&date forString:dateString range:nil error:&error]; 
+1
source share

All Articles