views:

466

answers:

3

Hello,

I am using the following code to try and display in a time in a table cell.

TimeSlot *timeSlot = [timeSlots objectAtIndex:indexPath.row];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSLog(@"Time: %@", timeSlot.time);
NSDate *mydate = timeSlot.time;
NSLog(@"Time: %@", mydate);
NSString *theTime = [timeFormat stringFromDate:mydate];
NSLog(@"Time: %@", theTime);

The log output is this:

  • 2010-04-14 10:23:54.626 MyApp[1080:207] Time: 2010-04-14T10:23:54
  • 2010-04-14 10:23:54.627 MyApp[1080:207] Time: 2010-04-14T10:23:54
  • 2010-04-14 10:23:54.627 MyApp[1080:207] Time: (null)

I am new to developing for the iPhone and as it all compiles with no errors or warnings I am at a loss as to why I am getting NULL in the log. Is there anything wrong with this code?

Thanks

Further Info

I used the code exactly from your answer lugte098 just to check and I was getting dates which leads me to believe that my TimeSlot class can't have a date correctly set in it's NSDate property. So my question becomes - how from XML do I set a NSDate property?

I have this code (abbreviated):

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
 if ([currentElement isEqualToString:@"Time"]) {
  currentTimeSlot.time = string
 }
}

Thanks

A: 

This might help:

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];
lugte098
Hi lugte098, I recognise this code it's what I used to base my code on. I know I have the mask wrong I will change it to what you have above but this again wasn't working and I was messing with the mask before I came to stackoverflow.
David A Gibson
A: 

you need to set

[timeFormat setDateFormat:@"dd MMMM YYYY"];

before

NSString *theTime = [timeFormat stringFromDate:mydate];

this line

mihirpmehta
Aren't I doing this already? The date mask is wrong as I was playing with it but I already have it in this order don't I? Am I missing something?
David A Gibson
you you given try this code... just insert that line... and see if it is working or not...
mihirpmehta
i am saying that instead of [timeFormat setDateFormat:@"HH:mm:ss"];try [timeFormat setDateFormat:@"dd MMMM YYYY"];
mihirpmehta
Hi, I have tried it and found the error was else where please see my additions, thanks
David A Gibson
+3  A: 

Based on the edited question:

You are setting a string from your XML into your timeSlot object. NSDateFormatter expects to be give an NSDate to format, so this is why your original question fails.

So you are asking a second question, which is how to create a a NSDate from an NSString. See the docs, you can use either dateFromString:, getObjectValue:forString:range:error: or getObjectValue:forString:errorDescription:. Therefore:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
currentTimeSlot.time = [df dateFomString:string];

And if it is nil, you should be using one of the error returning methods.

Paul Lynch
This is spot on thanks very much - was struggling to find the correct date mask but the 'T' part has solved that for me as well. Cheers paull!
David A Gibson