如何使用NSDate在Swift 3中获取时间(小时,分钟,秒)?

时间:2016-07-07 15:03:40

标签: nsdate swift3

如何确定Swift 3中NSDate类的小时,分​​钟和秒?

在Swift 2中:

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour

Swift 3?

9 个答案:

答案 0 :(得分:148)

Swift 3.0 中,Apple删除了“NS”前缀并简化了一切。以下是从“日期”类(NSDate备用)

获取小时,分钟和秒的方法
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)")

像这些你可以通过相应的方式获得时代,年,月,日期等。

答案 1 :(得分:66)

Swift 4.2& 5

// *** 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)")

Swift 3.0

// *** Create date ***
let date = Date()

// *** create calendar object ***
var calendar = NSCalendar.current

// *** Get components using current Local & Timezone ***    
print(calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date as Date))

// *** define calendar components to use as well Timezone to UTC ***
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
calendar.timeZone = TimeZone(identifier: "UTC")!

// *** Get All components from date ***
let components = calendar.dateComponents(unitFlags, 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)")

答案 2 :(得分:24)

let date = Date()       
let units: Set<Calendar.Component> = [.hour, .day, .month, .year]
let comps = Calendar.current.dateComponents(units, from: date)

答案 3 :(得分:17)

在Swift 3中你可以这样做,

//Your observable
Observable observable = Observable.just(...);

Observer<Object> observer1 = new Observer<Object>() { // ... }

PublishSubject<Object> observer2 = PublishSubject.create();
// observer1 will receive all onNext and onCompleted events
observer2.subscribe(observer1);

//observer2 will get items from observable and propagate them to observer1
observable.subscribe(observer2);

答案 4 :(得分:5)

快速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)
    }
}

用法:

print(Date().get(.year)) // => 2020
print(Date().get(.month)) // => 08
print(Date().get(.day)) // => 18 

答案 5 :(得分:4)

requirements.txt

答案 6 :(得分:2)

swift 4

==> Getting iOS device current time:-

print(" ---> ",(Calendar.current.component(.hour, from: Date())),":",
               (Calendar.current.component(.minute, from: Date())),":",
               (Calendar.current.component(.second, from: Date())))

output: ---> 10 : 11: 34

答案 7 :(得分:2)

为获得最佳效果,我创建了此功能:

func dateFormatting() -> String {
    let date = Date()
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEEE dd MMMM yyyy - HH:mm:ss"//"EE" to get short style
    let mydt = dateFormatter.string(from: date).capitalized

    return "\(mydt)"
}

您可以在任何需要的地方简单地调用它:

print("Date = \(self.dateFormatting())")

这是输出:

Date = Monday 15 October 2018 - 17:26:29

如果只想改变时间:

dateFormatter.dateFormat  = "HH:mm:ss"

这是输出:

Date = 17:27:30

就是这样...

答案 8 :(得分:0)

这对于那些想在多个课程中使用当前日期的人来说很方便。

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
}

}

用法

        var time = ""
        time = time.getCurrentTime()
        print(time)   // 1900-12-09-12-59