尝试这个,
NSString *Time=@"17:00:00";
NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:Time];
NSLog(@"date1 : %@", date1);
或者使用这些功能,
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
+(NSDateComponents *)componentsForDate:(NSDate *)date
{
if (date!=nil)
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dayComponents =
[gregorian components:DATE_COMPONENTS fromDate:date];
return dayComponents;
}
return nil;
}
+(NSString *)timeFromNSDate:(NSDate *)date
{
int hour=[self componentsForDate:date].hour;
int minute=[self componentsForDate:date].minute;
int second=[self componentsForDate:date].second;
NSString *returnDateString= [NSString stringWithFormat:@"%d:%d:%d",hour, minute,second];
return returnDateString;
}