views:

25318

answers:

2

searched for answers, but the one's i found didn't seem to be ipone specific.

I basically need to get current date and time separately, formatted as:

2009-04-26 
11:06:54

edit:

The code below, from another question on the same topic, generates

now:        |2009-06-01 23:18:23 +0100| 
dateString: |Jun 01, 2009 23:18| 
parsed:     |2009-06-01 23:18:00 +0100|

this is almost what i'm looking for, but i'd like to get just the date on one variable and just the time in another.

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];

NSDate *now = [[NSDate alloc] init];

NSString *dateString = [format stringFromDate:now];

NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"MMM dd, yyyy"];

NSDate *parsed = [inFormat dateFromString:dateString];

NSLog(@"\n"
"now:        |%@| \n"
"dateString: |%@| \n"
"parsed:     |%@|", now, dateString, parsed);
+8  A: 

iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.

And of course don't forget to release your date formatters when you're done with them. The above code leaks format, now, and inFormat.

Rob Napier
+3  A: 

this is what i used:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"\n"

   "theDate: |%@| \n"
   "theTime: |%@| \n"
   , theDate, theTime);

[dateFormat release];
[timeFormat release];
[now release];
Bernardo Moreira