Jon Sullivan
Jon Sullivan

Reputation: 399

NSDateFormatter not working for me?

Apparently "2013-02-27T11:01:00.000" isn't a valid date, because whenever I use the NSDateFormatter to edit a date in that format to just the time with AM/PM I get nothing.

   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   [formatter setDateFormat:@"HH:mm:ss"];
   NSString *time = [formatter stringFromDate:DepartTimeDate];

Does this mean I have to find a way to edit the date manually, if so, does anyone have any clue how this could be done?

Example: 2013-02-27T11:01:00.000 the "11:01" is all that I need, is there anyway to separate this from the rest of the "date", or string? Also, it is 24 hour time, so 1PM would be "2013-02-27T13:01:00.000"

If anyone has any clues, I'd be very thankful!

Upvotes: 0

Views: 786

Answers (1)

βhargavḯ
βhargavḯ

Reputation: 9836

Using some tricks , I have come up with following solution for your question.

    NSString *departTimeDate = @"2013-02-27T11:01:00.000";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS"];
    NSDate *date = [dateFormatter dateFromString:departTimeDate];
    [dateFormatter setDateFormat:@"HH:mm"];    
    NSLog(@"Expected Result___ %@",[dateFormatter stringFromDate:date]);

also please look at Date Formatting Guide.

Hope that this can give you a clue.

Upvotes: 1

Related Questions