ios 如何从 NSDate 获取小时和分钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2927028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get hour and minutes from NSDate?
提问by mac
In my application I need to get the hour and minute separately:
在我的应用程序中,我需要分别获取小时和分钟:
NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
int currentHourInNumber=[currentHour intValue];
Consider string1
contains 11:59:13 AM
which is coming from datepicker.
考虑来自日期选择器的string1
包含11:59:13 AM
。
Here if I use above code, it's okay to get hour if it's greater than 9. Else I need to change NSMakeRange(0,1)
to get hour between 1 to 9.
在这里,如果我使用上面的代码,如果它大于 9 就NSMakeRange(0,1)
可以得到小时。否则我需要更改以获取 1 到 9 之间的小时。
Are there any methods to get the hour, minutes, etc?
有什么方法可以获得小时,分钟等吗?
Thanks in advance, please provide me sample code.
提前致谢,请提供示例代码。
回答by Thomas Müller
Use an NSDateFormatterto convert string1
into an NSDate
, then get the required NSDateComponents:
使用NSDateFormatter转换string1
为NSDate
,然后获取所需的NSDateComponents:
Obj-C:
对象-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 and 2:
斯威夫特 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:
斯威夫特 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
More about the dateformatis on the official unicode site
有关日期格式的更多信息在官方 unicode 站点上
回答by Gil Margolin
If you only need it for presenting as a string the following code is much easier
如果您只需要它作为字符串呈现,则以下代码要容易得多
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];
回答by HenryRootTwo
This seems to me to be what the question is after, no need for formatters:
在我看来,这就是问题所在,不需要格式化程序:
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
回答by Paul Lynch
NSDateComponents
NSDateComponents
All you need can be found here:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
您需要的一切都可以在这里找到:https:
//developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html
回答by newDeveloper
With iOS 8, Apple introduced a helper method to retrieve the hour
, minute
, second
and nanosecond
from an NSDate object.
与iOS 8,苹果推出了一个辅助方法来检索hour
,minute
,second
并nanosecond
从一个NSDate对象。
Objective-C
目标-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);
Swift
迅速
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)")
}
回答by sketchyTech
If you were to use the C library then this could be done:
如果您要使用 C 库,则可以这样做:
time_t t;
struct tm * timeinfo;
time (&t);
timeinfo = localtime (&t);
NSLog(@"Hour: %d Minutes: %d", timeinfo->tm_hour, timeinfo->tm_min);
And using Swift:
并使用 Swift:
var t = time_t()
time(&t)
let x = localtime(&t)
println("Hour: \(x.memory.tm_hour) Minutes: \(x.memory.tm_min)")
For further guidance see: http://www.cplusplus.com/reference/ctime/localtime/
如需进一步指导,请参阅:http: //www.cplusplus.com/reference/ctime/localtime/
回答by yo2bh
Swift 2.0
斯威夫特 2.0
You can do following thing to get hours and minute from a 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
回答by Juan Boero
Swift 2.0
斯威夫特 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))
Please do take note of the cast to String in the print statement, you can easily assign that value to variables, like this:
请注意打印语句中对 String 的强制转换,您可以轻松地将该值分配给变量,如下所示:
var hoursString = String(hour)
var minutesString = String(minute)
Then you can concatenate values like this:
然后你可以像这样连接值:
var compoundString = "\(hour):\(minute)"
print(compoundString)