我如何从NSDate获得小时和分钟?
在我的应用程序中,我需要分别获取小时和分钟:
NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)]; int currentHourInNumber=[currentHour intValue];
考虑string1
包含从11:59:13 AM
来的11:59:13 AM
。
在这里,如果我使用上面的代码,它可以得到小时,如果它是大于9.否则我需要更改NSMakeRange(0,1)
以获得1至9之间的小时。
有什么方法来获得小时,分钟等?
在此先感谢,请提供示例代码。
使用NSDateFormatter将string1
转换为NSDate
,然后获取所需的NSDateComponents :
Obj-C :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"<your date format goes here"]; NSDate *date = [dateFormatter dateFromString:string1]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute];
斯威夫特1和2 :
let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "Your date Format" let date = dateFormatter.dateFromString(string1) let calendar = NSCalendar.currentCalendar() let comp = calendar.components([.Hour, .Minute], fromDate: date) let hour = comp.hour let minute = comp.minute
Swift 3 :
let dateFormatter = DateFormatter() dateFormatter.dateFormat = "Your date Format" let date = dateFormatter.date(from: string1) let calendar = Calendar.current let comp = calendar.dateComponents([.hour, .minute], from: date) let hour = comp.hour let minute = comp.minute
关于dateformat的更多信息在官方的unicode网站上
如果你只需要它作为一个string呈现,下面的代码更容易
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm"]; NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];
这在我看来是问题之后,不需要格式化程序:
NSDate *date = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; NSInteger hour = [components hour]; NSInteger minute = [components minute];
NSDateComponents
,所有你需要的可以在这里find: http : //developer.apple.com/mac/library/documentation/cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
在iOS 8中,Apple引入了一个辅助方法来从NSDate对象中检索hour
, minute
, second
和nanosecond
。
Objective-C的
NSDate *date = [NSDate currentDate]; NSInteger hour = 0; NSInteger minute = 0; NSCalendar *currentCalendar = [NSCalendar currentCalendar]; [currentCalendar getHour:&hour minute:&minute second:NULL nanosecond:NULL fromDate:date]; NSLog(@"the hour is %ld and minute is %ld", (long)hour, (long)minute);
迅速
let date = NSDate() var hour = 0 var minute = 0 let calendar = NSCalendar.currentCalendar() if #available(iOS 8.0, *) { calendar.getHour(&hour, minute: &minute, second: nil, nanosecond: nil, fromDate: date) print("the hour is \(hour) and minute is \(minute)") }
如果你要使用C库,那么可以这样做:
time_t t; struct tm * timeinfo; time (&t); timeinfo = localtime (&t); NSLog(@"Hour: %d Minutes: %d", timeinfo->tm_hour, timeinfo->tm_min);
并使用Swift:
var t = time_t() time(&t) let x = localtime(&t) println("Hour: \(x.memory.tm_hour) Minutes: \(x.memory.tm_min)")
有关进一步指导,请参阅: http : //www.cplusplus.com/reference/ctime/localtime/
Swift 2.0
let dateNow = NSDate() let calendar = NSCalendar.currentCalendar() let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow) let minute = calendar.component(NSCalendarUnit.Minute, fromDate: dateNow) print(String(hour)) print(String(minute))
请注意在打印语句中将String转换为String,您可以轻松地将该值赋予variables,如下所示:
var hoursString = String(hour) var minutesString = String(minute)
然后你可以连接这样的值:
var compoundString = "\(hour):\(minute)" print(compoundString)
Swift 2.0
你可以做以下的事情从date中获得小时和分钟:
let dateFromat = NSDateFormatter() dateFromat.dateFormat = "hh:mm a" let date = dateFromat.dateFromString(string1) // In your case its string1 print(date) // you will get - 11:59 AM