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);
3
  • NSTimeZone knownTimeZones returns the list of recognized timezones. Odds are "PET" is not in the list.
    – Hot Licks
    Sep 30, 2014 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, 2014 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".
    – Hot Licks
    Sep 30, 2014 at 15:56

2 Answers 2

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;
   }
0
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

8
  • Initially I dont have NSDate. so using NSDateFormatter only i can convert NSString into NSDate.
    – christijk
    Sep 30, 2014 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, 2014 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, 2014 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, 2014 at 13:10
  • 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, 2014 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.