在我的申请中,我需要分别得到小时和分钟:
NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
int currentHourInNumber=[currentHour intValue];
考虑string1
包含来自datepicker的11:59:13 AM
。
如果我使用上面的代码,如果它大于9,则可以获得小时。否则我需要更改NSMakeRange(0,1)
以获得1到9之间的小时。
有没有方法可以获得小时,分钟等?
提前致谢,请提供示例代码。
答案 0 :(得分:256)
使用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];
Swift 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网站
答案 1 :(得分:49)
如果您只需要它作为字符串呈现,则以下代码更容易
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];
答案 2 :(得分:45)
在我看来,这就是问题所在,不需要格式化程序:
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
答案 3 :(得分:6)
NSDateComponents
您可以在这里找到所有需要:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
答案 4 :(得分:4)
如果你要使用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/
答案 5 :(得分:4)
在iOS 8中,Apple推出了一种帮助方法,用于从NSDate对象中检索hour
,minute
,second
和nanosecond
。
目标-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)")
}
答案 6 :(得分:2)
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))
请注意print语句中对String的强制转换,您可以轻松地将该值赋给变量,如下所示:
var hoursString = String(hour)
var minutesString = String(minute)
然后你可以连接像这样的值:
var compoundString = "\(hour):\(minute)"
print(compoundString)
答案 7 :(得分:2)
Swift 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