0

I am getting the date from data base in this format, '01/02/2013 17:00'. I have to detach the date and time and put it in separate columns in the nstableview (date in date column and time in time column). How should i do this?

user1999892
  • 135
  • 1
  • 8

3 Answers3

4

You can:

NSArray *dateAndTime = [@"01/02/2013 17:00" componentsSeparatedByString:@" "];

This if you are certain about that format. Otherwise you should be careful and rely on something like NSDateFormatter.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
0
NSString *dateTime = @"01/02/2013 17:00";

NSArray *strings = [dateTime componentsSeparatedByString:@" "];

The array strings will now look like...

[
    @"01/02/2013",
    @"17:00"
]
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

If you want to be able to change to time format, or want more control over the times etc. I recommend a NSDateFormatter..

    NSString *dateString = @"'01/02/2013 17:00";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/YYYY HH:mm"];

    NSDate *date = [formatter dateFromString:dateString];

    [formatter setDateFormat:@"dd/MM/YYYY"];
    NSString *newDateString = [formatter stringFromDate:date];
    [formatter setDateFormat:@"HH:mm"];
    NSString *newTimeString = [formatter stringFromDate:date];
elk
  • 31
  • 6