ios 如何使用 Swift 获取一天中的时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24137692/
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 to get the hour of the day with Swift?
提问by Samuel Gómez
How would I get the hour of the day in Swift.
I have tried NSCalendar
and NSDateComponents
, but I'm afraid I'm just starting with Swift.
我将如何在 Swift 中获得一天中的时间。我已经尝试过NSCalendar
和NSDateComponents
,但恐怕我才刚刚开始使用 Swift。
回答by average Joe
Swift 5.0 / 4.0 / 3.0
斯威夫特 5.0 / 4.0 / 3.0
let hour = Calendar.current.component(.hour, from: Date())
Or, if you're interested in 12 hour AM/PM
date format, then use NSDateFormatter
或者,如果您对12 hour AM/PM
日期格式感兴趣,则使用NSDateFormatter
let formatter = DateFormatter()
formatter.dateFormat = "hh a" // "a" prints "pm" or "am"
let hourString = formatter.string(from: Date()) // "12 AM"
If you want minutes, seconds and others, do as following
如果你想要分钟,秒等,请执行以下操作
let date = Date() // save date, so all components use the same date
let calendar = Calendar.current // or e.g. Calendar(identifier: .persian)
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
Check out available components on Apple docs:
查看Apple 文档上的可用组件:
.era, .year, .month, .day, .hour, .minute, .second,
.weekday, .weekdayOrdinal, .quarter, weekOfMonth, .weekOfYear,
.yearForWeekOfYear, .nanosecond, .calendar, .timezone
Swift 2.0
斯威夫特 2.0
let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())
Swift 1.0
斯威夫特 1.0
let hour = NSCalendar.currentCalendar().component(.CalendarUnitHour, fromDate: NSDate())
回答by Kumar KL
Swift 3:
斯威夫特 3:
let date = Date()// Aug 25, 2017, 11:55 AM
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date) //11
let minute = calendar.component(.minute, from: date) //55
let sec = calendar.component(.second, from: date) //33
let weekDay = calendar.component(.weekday, from: date) //6 (Friday)
Get any of component available from the API below
从下面的 API 获取任何可用的组件
public enum Component {
case era
case year
case month
case day
case hour
case minute
case second
case weekday
case weekdayOrdinal
case quarter
case weekOfMonth
case weekOfYear
case yearForWeekOfYear
case nanosecond
case calendar
case timeZone
}
Swift 2:
斯威夫特 2:
let currentHour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate())
This could be enough :
这可能就足够了:
let currentDate = NSDate() // You can input the custom as well
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: currentDate)
let currentHour = components.hour // You can play around with the ""components""
回答by Binarian
If you want the current hour in a String, this is as short and readable I could think of.
如果你想要字符串中的当前小时,这是我能想到的那么简短和可读。
let formatter = NSDateFormatter()
formatter.dateFormat = "HH"
let timeString = formatter.stringFromDate(NSDate())
回答by Samuel Gómez
Finally I was able to find the easiest solution after struggling for a time
经过一段时间的挣扎,终于找到了最简单的解决方案
let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.HourCalendarUnit, fromDate: NSDate())
let nHourOfDay = dateComponents.hour
回答by Gmeister4
For Swift 2.0:
对于 Swift 2.0:
let hour = NSCalendar.currentCalendar().component(NSCalendarUnit.Hour, fromDate: NSDate())
回答by J-Dizzle
Here is a reference example for how I do it (DateUtils.swift) -
这是我如何做的参考示例(DateUtils.swift)-
Example Use:
使用示例:
let today = DateUtils.getToday();
let hr = DateUtils.getHours(today);
let min = DateUtils.getMinutes(today);
... (etc.) ...
DateUtils.swift:
DateUtils.swift:
//Field wrapper routines
class func getToday() -> Date { return Date(); }
class func getHours(_ date : Date) -> Int { return Calendar.current.component(.hour, from: date); }
class func getMinutes(_ date : Date) -> Int { return Calendar.current.component(.minute, from: date); }
... (continued for all fields, see file) ...
回答by J. Cross
You can get the integer value of the current hour in one step like this:
您可以像这样一步一步地获取当前小时的整数值:
let currentHour = NSCalendar.currentCalendar().components(.Hour, fromDate: NSDate()).hour