Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Aug 29, 2008, 04:13 PM   #1
macrumors member
Join Date: Jun 2008
Date: From String to Object, Back to String

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.
Fontano is offline   0 Reply With Quote
Old Aug 29, 2008, 04:34 PM   #2
macrumors 68040
lee1210's Avatar
Join Date: Jan 2005
Location: 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:
The date is: 2008-08-29T14:00:00.000US/Central!
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.

Last edited by lee1210; Aug 29, 2008 at 04:39 PM.
lee1210 is offline   0 Reply With Quote
Old Aug 29, 2008, 04:45 PM   #3
Thread Starter
macrumors member
Join Date: Jun 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)
Fontano is offline   0 Reply With Quote
Old Aug 29, 2008, 07:23 PM   #4
Moderator emeritus
kainjow's Avatar
Join Date: Jun 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.
kainjow is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 10:45 PM
Resolved: Best way to access all properties of an object whose names begin with a string? moonman239 iPhone/iPad Programming 7 Nov 18, 2013 03:04 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 10:42 AM
Creating Object From String hego555 Mac Programming 8 Feb 12, 2013 10:44 PM
Problem in String to Date conversion Narendar Singh iPhone/iPad Programming 4 Oct 1, 2012 12:58 PM

Forum Jump

All times are GMT -5. The time now is 07:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC