Date: From String to Object, Back to String

Discussion in 'Mac Programming' started by Fontano, Aug 29, 2008.

  1. Fontano macrumors member

    Jun 27, 2008
    Why is it that the seemingly simple things, cause you the most headaches.
    Maybe it is just the day long pounding on the hard stuff, is making something that should be simple... more harder then it is.

    Here is my issue/problem:

    From an XML file, I have a Date-Time stated as:
    Example: 2008-08-29T19:00:00.000Z

    Where Z is GMT...

    I can read it fine from the XML and get it into a NSString object in my class.

    Now, ultimately I need to go from that stored value to a Date/Time (right now just time) string value, but for a DIFFERENT timzone (my local timezone).

    So in my class, I built a method to return that string as a NSCalendarDate object.

    -(NSCalendarDate *)getXMLDateTimeAsCalendarDate
    	return [NSCalendarDate dateWithString:[_XMLDateTime stringByReplacingOccurrencesOfString:@"Z" withString:@"GMT"] calendarFormat:@"%Y-%m-%dT%H:%M:%S.%F%Z"];
    And this does appear to be return the object with the correct date/time in GMT.

    But now I am stuck.
    I don't know how to go the last step, to go from the NSCalendarDate object to a string, adjusted for my local timezone.

    I have tried a few things, and I am getting to the point of frustration, so I am going to pack it up for today... but thought maybe I could ask here for some direction... as I know it has to be something simple I am just missing from the documentations.


    Start: NSString "2008-08-29T19:00:00.000Z"
    End: NSString "2:00 pm"
    (Central Daylight Savings Timezone)

    If I can get it to the END style above, I am pretty sure then from the code I will be able to figure out how to get it to: "08/29/2008 2:00pm" and other formats.

    Thanks for the help.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I just threw this together, there might be a better way, a superior style, etc. but this should get you a date that's ready to pull apart to get what you need:

    #import <Foundation/Foundation.h>
    int main (int argc, const char * argv[]) {
      if(argc < 2) return -1;
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      NSString *myDateStr = [[NSString alloc] initWithCString:argv[1] encoding:NSASCIIStringEncoding];
      NSCalendarDate *myDate = [NSCalendarDate dateWithString:myDateStr calendarFormat:@"%Y-%m-%dT%H:%M:%S.%F%Z"];
      [myDate setTimeZone:[[NSCalendarDate calendarDate] timeZone]];
      NSLog(@"The date is: %@!",myDate);
      [pool release];
      return 0;

    I ran this with:
    As the only argument, and got:
    As far as I can tell, you can now use myDate to pull out the time, etc.


    P.S. Yes, the simple stuff doesn't seem like it after a while. If I can do it with Cocoa, it must be simple. =)
    P.P.S. I'm not totally sure why I didn't just hardcode an NSString with the value to test with instead of using argv[] (feels dirty in Objective-C). Oh well.
  3. Fontano thread starter macrumors member

    Jun 27, 2008
    If you just heard a thunk.... that is my head hitting the desk.

    I was so close in one of my attempts...

    Thank you very much.
    If anyone else has another variation on this, I would like to see it also (even though this is pretty straight forward)
  4. kainjow Moderator emeritus


    Jun 15, 2000
    If you are targeting 10.4+, you should use NSCalendar/NSDateComponents instead of NSCalendarDate, as there's a good chance NSCalendarDate will be deprecated in 10.6.

Share This Page