-5

I would like to convert 2013-07-10T21:48:09.000Z to 10.7.2013 using nsdateformatter.

I've tried going through some tutorial but I can't seem to figure out what to do, How would I correctly do this conversion?

Any help is greatly appreciated.

I tried this but I only get returned a (null)

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:000Z"];
    NSDate *theDate = [dateFormatter dateFromString:stringDate];
    NSLog(@"%@",[dateFormatter stringFromDate:theDate]);
IamGretar
  • 71
  • 9
  • please link to the tutorial you used – Stephan Jul 12 '13 at 18:17
  • Have a look here, for example: http://stackoverflow.com/questions/3094819/nsdateformatter-returning-nil-in-os-4-0 – onitake Jul 12 '13 at 18:24
  • There's a seconds part missing in your format. – Eimantas Jul 12 '13 at 18:38
  • What part am I missing ? – IamGretar Jul 12 '13 at 18:39
  • `Z` needs to have quotes around it if you want it to be a literal "Z", because `Z` is used as a timezone format character. – Dave DeLong Jul 12 '13 at 18:41
  • 2
    Read the spec for NSDateFormatter. And follow the links in there through the several pages to where the format strings are described. Then search SO for the literally thousands of questions about NSDateFormatter, written by previous folks who, like you, failed to do this fundamental research. – Hot Licks Jul 12 '13 at 19:24
  • 3
    In your above code snippet did you do the minimal amount of debugging required to see if you're getting a good result from the `dateFromString` operation, to divide your problem in half? – Hot Licks Jul 12 '13 at 19:25

1 Answers1

1

First, your date format is wrong -- the second line should be [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.000Z"]; -- that is, you need the ss. after the mm: but before the 000Z. This will give you the correct NSDate.

Next, you need to create a new date formatter with the format @"dd.M.yyyy" (or @"d.M.yyyy" if you want to remove the leading zero if the day is a single digit as well) and call stringFromDate: on that for the NSLog.

Just to recap, the whole chunk of code would be

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.000Z"];
NSDate *theDate = [dateFormatter dateFromString:stringDate];

NSDateFormatter *printFormatter = [[NSDateFormatter alloc] init];
[printFormatter setDateFormat:@"dd.M.yyyy"];
NSLog(@"%@",[printFormatter stringFromDate:theDate]);
gtmtg
  • 3,010
  • 2
  • 22
  • 35
  • It returned '2013-07-10T21:48:09.000+0000', I need it to return '10.7.2013' – IamGretar Jul 12 '13 at 18:47
  • @IamGretar Oops, didn't catch that -- I thought you just wanted the correct NSDate. I've updated my answer. – gtmtg Jul 12 '13 at 18:58
  • Doesn't need to be a new NSDateFormatter -- simply set a new format into the old one. – Hot Licks Jul 12 '13 at 19:26
  • @HotLicks Good point, that works as well (and saves you the trouble of allocating another ```NSDateFormatter``` instance). – gtmtg Jul 12 '13 at 19:37