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?
Asked
Active
Viewed 49 times
0
-
What you have tried and where you are facing problem? – CRDave Feb 06 '13 at 10:20
3 Answers
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