在我的应用程序需要分别得到小时和分钟:
NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
int currentHourInNumber=[currentHour intValue];
考虑string1
包含11:59:13 AM
这是从日期选择器来了。
在这里,如果我用上面的代码,也没关系拿到小时,如果是大于9否则我需要改变NSMakeRange(0,1)
1之间取得小时到9。
是否有任何方法来获取小时,分钟,等?
在此先感谢,请给我提供的示例代码。
Answer 1:
使用一个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
斯威夫特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
更多关于日期格式是在官方网站的unicode
Answer 2:
如果你只需要它表现为一个字符串下面的代码更容易
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];
Answer 3:
在我看来这是问题是什么之后,无需格式化:
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
Answer 4:
NSDateComponents
所有你需要的可以在这里找到:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
Answer 5:
如果你使用C库,然后可以这样做:
time_t t;
struct tm * timeinfo;
time (&t);
timeinfo = localtime (&t);
NSLog(@"Hour: %d Minutes: %d", timeinfo->tm_hour, timeinfo->tm_min);
并利用斯威夫特:
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/
Answer 6:
与iOS 8,苹果推出一个辅助方法来检索hour
, minute
, second
和nanosecond
从一个NSDate对象。
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)")
}
Answer 7:
雨燕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))
请注意要转换成字符串的print语句,你可以轻松地分配该值的变量,就像这样:
var hoursString = String(hour)
var minutesString = String(minute)
然后,您可以连接这样的价值观:
var compoundString = "\(hour):\(minute)"
print(compoundString)
Answer 8:
雨燕2.0
你可以做以下的事情拿到小时和分钟从日期:
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
文章来源: How do I get hour and minutes from NSDate?