从 NSDate 中提取日期和时间 - Objective C
Extract Date and Hour from NSDate - Objective C
我有 dateString
这种格式 "mm/dd/yyyy '-' HH:mm"
。
我想提取字符串中的日期和字符串中的小时。
我尝试了以下代码,但是 return 日期错误:
- (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]];
}
示例字符串:"05/01/2015 - 11:15"
结果:日期:1/1/2015
,时间:11:15
还有我如何维护这种日期格式 dd/mm/yyyy
,我的意思是当日期是 1
时,它显示 01
未测试但是:
MM
这个月。不是 mm
,它是分钟。
您已经有了 NSDateFormatter
那么为什么不再使用它来代替 NSDateComponents
和 NSString
格式呢?
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy '-' HH:mm"];
NSDate *date = [formatter dateFromString:dateString];
//Now you have the correct date. So we'll use the NSDateFormatter to transform NSDate (date) into NSString according to the format we need.
[formatter setDateFormat:@"MM/dd/yyyy"];
dateLabel = [formatter stringFromDate:date];
[formatter setDateFormat:@"HH:mm"];
hourLabel = [formatter stringFromDate:date];
hourLabel
/dateLabel
因为 NSString
而不是 UILabel
是一个误导性的 var 命名。
我有 dateString
这种格式 "mm/dd/yyyy '-' HH:mm"
。
我想提取字符串中的日期和字符串中的小时。
我尝试了以下代码,但是 return 日期错误:
- (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]];
}
示例字符串:"05/01/2015 - 11:15"
结果:日期:1/1/2015
,时间:11:15
还有我如何维护这种日期格式 dd/mm/yyyy
,我的意思是当日期是 1
时,它显示 01
未测试但是:
MM
这个月。不是 mm
,它是分钟。
您已经有了 NSDateFormatter
那么为什么不再使用它来代替 NSDateComponents
和 NSString
格式呢?
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy '-' HH:mm"];
NSDate *date = [formatter dateFromString:dateString];
//Now you have the correct date. So we'll use the NSDateFormatter to transform NSDate (date) into NSString according to the format we need.
[formatter setDateFormat:@"MM/dd/yyyy"];
dateLabel = [formatter stringFromDate:date];
[formatter setDateFormat:@"HH:mm"];
hourLabel = [formatter stringFromDate:date];
hourLabel
/dateLabel
因为 NSString
而不是 UILabel
是一个误导性的 var 命名。