I have dateString
with this format "mm/dd/yyyy '-' HH:mm"
.
I want to extract the date in string and hour in string.
I've tried the following code but this return a wrong date:
- (void) extractDate: (NSString *)dateString intoHour: (NSString *)hourLabel and: (NSString*)dateLabel {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"mm/dd/yyyy '-' HH:mm"];
NSData *date = [formatter dateFromString:dateString];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date];
hourLabel = [NSString stringWithFormat:@"%lu:%lu", [components hour], [components minute]];
dateLabel = [NSString stringWithFormat:@"%lu/%lu/%lu", [components day], [components month], [components year]];
}
Exemple String: "05/01/2015 - 11:15"
Result: date: 1/1/2015
, time: 11:15
and also how can I maintain this date format dd/mm/yyyy
, I mean when the day is 1
, it shows 01