如何确定Swift 3中NSDate类的小时,分钟和秒?
在Swift 2中:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
Run Code Online (Sandbox Code Playgroud)
斯威夫特3?
SaR*_* DM 154
在Swift 3.0中, Apple删除了"NS"前缀并使一切变得简单.以下是从'Date'类获取小时,分钟和秒的方法(NSDate alternate)
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("hours = \(hour):\(minutes):\(seconds)")
Run Code Online (Sandbox Code Playgroud)
像这些你可以通过相应的方式获得时代,年,月,日期等.
Dip*_*ara 76
Swift 3.0
// *** Create date ***
let date = Date()
// *** create calendar object ***
var calendar = Calendar.current
// *** Get components using current Local & Timezone ***
print(calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date))
// *** define calendar components to use as well Timezone to UTC ***
calendar.timeZone = TimeZone(identifier: "UTC")!
// *** Get All components from date ***
let components = calendar.dateComponents([.hour, .year, .minute], from: date)
print("All Components : \(components)")
// *** Get Individual components from date ***
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("\(hour):\(minutes):\(seconds)")
Run Code Online (Sandbox Code Playgroud)
小智 24
let date = Date()
let units: Set<Calendar.Component> = [.hour, .day, .month, .year]
let comps = Calendar.current.dateComponents(units, from: date)
Run Code Online (Sandbox Code Playgroud)
Ham*_*med 21
斯威夫特4
let calendar = Calendar.current
let time=calendar.dateComponents([.hour,.minute,.second], from: Date())
print("\(time.hour!):\(time.minute!):\(time.second!)")
Run Code Online (Sandbox Code Playgroud)
小智 17
在Swift 3中你可以这样做,
let date = Date()
let hour = Calendar.current.component(.hour, from: date)
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5+
extension Date {
func get(_ type: Calendar.Component)-> String {
let calendar = Calendar.current
let t = calendar.component(type, from: self)
return (t < 10 ? "0\(t)" : t.description)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
print(Date().get(.year)) // => 2020
print(Date().get(.month)) // => 08
print(Date().get(.day)) // => 18
Run Code Online (Sandbox Code Playgroud)
小智 5
let hours = time / 3600
let minutes = (time / 60) % 60
let seconds = time % 60
return String(format: "%0.2d:%0.2d:%0.2d", hours, minutes, seconds)
Run Code Online (Sandbox Code Playgroud)
对于那些想要在多个类中使用当前日期的人来说,这可能很方便。
extension String {
func getCurrentTime() -> String {
let date = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
let realTime = "\(year)-\(month)-\(day)-\(hour)-\(minutes)-\(seconds)"
return realTime
}
}
Run Code Online (Sandbox Code Playgroud)
用法
var time = ""
time = time.getCurrentTime()
print(time) // 1900-12-09-12-59
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
110753 次 |
| 最近记录: |