NSDateFormatter Issues

Discussion in 'iOS Programming' started by nomar383, May 18, 2009.

  1. nomar383 macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #1
    Okay, I'm sick of playing with this. Break it to me, what's up? This compiles fine, but I can't seem to get the date formatter to work. That NSLog displays nothing.

    Code:
    //create formatter and format
    	NSDateFormatter *dtsFormatter = [[[NSDateFormatter alloc] init]  autorelease];
    	[dtsFormatter setDateFormat:@"%j"];
    	
    	//run the string through the formatter
    	NSDate *testDate = [NSDate date];
    	NSString *formattedDate = [dtsFormatter stringFromDate:testDate];
    	NSLog(formattedDate);
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
  3. nomar383 thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #3
    Well, what is the proper formatter to get the day of the year in a 001-366 format? I've used the "%j" convention with other objective c classes and it has worked... :(
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Follow the link I provided to find the answer...
     
  5. nomar383 thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #5
    Beautiful, I got it working :)

    Just needed to replace the %j with D, thanks!
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Actually, if you want 001-366, you'll want @"DDD".
     
  7. nomar383 thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #7
    Really? It seems to be working with one D...

    By the way, when you get the day of the week using the same method, it returns 1-7 rather than 0-6 as was the case with the formatter I used in previous versions of my app. Interesting

    EDIT: DDD also works, so what is the difference?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Leading zeros.
     
  9. nomar383 thread starter macrumors 65816

    nomar383

    Joined:
    Jan 29, 2008
    Location:
    Rexburg, ID
    #9
    Thanks for the clarification, and your time on this one
     

Share This Page