1

I want parse date strings from API with time zone PET. So I created NSDateFormatter and converting the string into date but unfortunately its not working. I am getting nil as the result. Any workarounds ?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"EEE MMM dd HH:mm:ss yyyy zz"];
NSDate *newStartDate = [formatter dateFromString:@"Mon Sep 29 14:40:00 2014 PET"];

NSLog(@"newStartDate - %@",newStartDate);
christijk
  • 1,753
  • 18
  • 26
  • NSTimeZone knownTimeZones returns the list of recognized timezones. Odds are "PET" is not in the list. – Hot Licks Sep 30 '14 at 12:47
  • But using [NSTimeZone timeZoneWithAbbreviation] will get a list, where PET is there. If we use [NSTimeZone knownTimeZoneNames]; then "America/Lima" is PET – christijk Sep 30 '14 at 12:49
  • 1
    NSDateFormatter tends to be fussy about reading formats with "EEE" in them -- sometimes it works, sometimes not. It's best to strip the day-of-week value from the string before parsing (and, of course, suitably modify the format string). Also, be aware of the ["locale feature"](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature). – Hot Licks Sep 30 '14 at 15:56

2 Answers2

2

Try with the below function.It will Work...

     NSDate *newStartDate = [self dateFromString:@"Mon Sep 29 14:40:00 2014 PET"];

     NSLog(@"newStartDate - %@",newStartDate);


    - (NSDate *)dateFromString:(NSString*)inDateString
    {
       NSDateFormatter  *pivotalDateFormatter = [[NSDateFormatter alloc] init];
       NSDate * dateToBeReturned = nil;
       [pivotalDateFormatter setDateFormat:@"EEE MMM dd HH':'mm':'ss yyyy z"];

       NSString * timezoneAbbreviation = [[inDateString componentsSeparatedByString:@" "]       lastObject];
       NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:timezoneAbbreviation];
      if (timeZone)
      {
        NSString *gmtTime = [inDateString    stringByReplacingOccurrencesOfString:timezoneAbbreviation withString:@"GMT"];
        dateToBeReturned = [[pivotalDateFormatter dateFromString:gmtTime] dateByAddingTimeInterval:-timeZone.secondsFromGMT];
      }
    return dateToBeReturned;
   }
christijk
  • 1,753
  • 18
  • 26
CKR666
  • 1,497
  • 1
  • 13
  • 10
0

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

NSDate *newStartDate = [NSDate date];

NSString *newStartDateStr = [dateFormatter stringFromDate:newStartDate];

now change the dateformatter dateformat

[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss yyyy zz"]; //set dateformat as per ur requirement

newStartDate = [dateFormatter dateFromString:newStartDateStr];

NSLog(@"%@", newStartDate);

u will get the dateformat u required

happy coding

sreekanthk
  • 362
  • 2
  • 21
  • Initially I dont have NSDate. so using NSDateFormatter only i can convert NSString into NSDate. – christijk Sep 30 '14 at 13:01
  • generally dateformatter has some default dateformat...so u first have to convert your date to string in that dateformat and then afterwards changing the dateformat u will get ur required dateformat – sreekanthk Sep 30 '14 at 13:03
  • As I told Initially I dont have date, i have only string , so i need to convert that string into date?? – christijk Sep 30 '14 at 13:07
  • take that string into a date variable without setting any dateformat to dateformatter, then transfer that date to another string. set ur required dateformat. and get ur required date from that string – sreekanthk Sep 30 '14 at 13:10
  • If we do like that NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *dateTemp = [dateFormatter dateFromString:@"Mon Sep 29 14:40:00 2014 PET"]; Now the dateTemp is nil. – christijk Sep 30 '14 at 13:14
  • because as i've said that ur dateformatter has some default dateformat. – sreekanthk Sep 30 '14 at 13:18
  • I created a new dateformatter NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; without specifying any format. This is my string "Mon Sep 29 14:40:00 2014 PET" – christijk Sep 30 '14 at 13:20
  • 1
    @sreekanthk - You're making no sense. When you supply an explicit date format it overrides any "default format". And converting from NSDate to string and back to NSDate proves nothing. – Hot Licks Sep 30 '14 at 16:05