-1

I have date in NSString format as "Wed Jul 29 14:46:11 +0000 2015". I have tried all possible ways of converting this string into NSDate. But couldn't find the right format for type of string.

Can someone help out or give suggestion to convert this into NSDate.

Below is the code:

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat       = @"EEEE MM dd HH:mm:ss Z YYYY";
    //[format setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
    [format setLocale:locale];
       NSDate *date = [format dateFromString:@"Wed Aug 19 17:36:46 +0000 2015"];
    NSLog(@"converted date is %@",date);* 

But its returning wrong date as converted date is 2014-12-24 17:36:46 +0000

Thanks.

RamChandraReddy
  • 239
  • 2
  • 12

3 Answers3

2

You need to set your NSDateFormatter's dateFormat to EEEE MM dd HH:mm:ss Z YYYY.

You can use the following code for converting the specified string to date.

Objective C:

NSString *string        = @"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat       = @"EEEE MMM dd HH:mm:ss Z yyyy";
NSDate *date            = [format dateFromString:string];

Swift:

var dateStr          = "Wed Jul 29 14:46:11 +0000 2015"
var formatter        = NSDateFormatter();
formatter.dateFormat = "EEEE MMM dd HH:mm:ss Z yyyy"
var date             = formatter.dateFromString(dateStr)
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • don't forget to set the locale to en_US_POSIX. Otherwise it will fail on locales that have a different word for Wednesday. – Matthias Bauch Jul 31 '15 at 17:41
  • NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"EEEE MM dd HH:mm:ss Z YYYY"; //[format setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [format setLocale:locale]; NSDate *date = [format dateFromString:@"Wed Aug 19 17:36:46 +0000 2015"]; NSLog(@"converted date is %@",date); But its returning wrong date as converted date is 2014-12-24 17:36:46 +0000 – RamChandraReddy Aug 20 '15 at 07:39
  • @RamChandraReddy: There was one issue with the conversion year representation, corrected it, please check the updated answer – Midhun MP Aug 21 '15 at 11:20
0

Are you sure that:

  • you have the right format for NSDateFormatter when calling dateFromString()?
  • you have the right locale?
  • you have looked up any errors returned?

Please post the code and your failure message/ results. See documentation and related SO post

Community
  • 1
  • 1
rholmes
  • 4,064
  • 3
  • 25
  • 34
0

You can follow this. It helps you.

NSString *dateString = @"Wed Jul 29 14:46:11 +0000 2015";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE MMMM dd yyyy HH:mm:ss Z yyyy"];

NSDate *dateFromString = [[NSDate alloc] init];

dateFromString = [dateFormatter dateFromString:dateString];
iSara
  • 919
  • 3
  • 10
  • 24