2016-07-07 22 views
44

Làm thế nào bạn có thể xác định giờ, phút và giây từ lớp NSDate trong Swift 3?Làm thế nào để có được thời gian (giờ, phút, giây) trong Swift 3 bằng cách sử dụng NSDate?

Trong Swift 2:

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

Swift 3?

+0

Xem thêm [câu trả lời này] (http: // stackoverflow .com/a/33343958/3681880). – Suragch

+1

Có thể trùng lặp [Làm thế nào để có được thời gian hiện tại là datetime] (http://stackoverflow.com/questions/24070450/how-to-get-the-current-time-as-datetime) – Moritz

Trả lời

121

Trong Swift 3.0 Apple đã xóa tiền tố 'NS' và làm mọi thứ đơn giản. Dưới đây là cách lấy giờ, phút và giây từ lớp 'Ngày' (NSDate thay thế)

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

Giống như các bạn có thể lấy thời đại, năm, tháng, v.v ... bằng cách chuyển tương ứng.

55

Swift 3,0

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

// *** 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 as Date) 
print("All Components : \(components)") 

// *** Get Individual components from date *** 
let hour = calendar.component(.hour, from: date as Date) 
let minutes = calendar.component(.minute, from: date as Date) 
let seconds = calendar.component(.second, from: date as Date) 
print("\(hour):\(minutes):\(seconds)") 
+0

Nếu tôi nhận được '' Component 'không phải là kiểu thành viên của' Calendar'' trên dòng 'let unitFlags ...' ' – netigger

+1

Những gì bạn đang hỏi không rõ ràng với tôi, Làm thế nào bạn có thể thêm kiểu không phải thành viên trong' Calendar.Component'? Hãy xem xét ngày của bạn là sau "02/12/15, 16:46" trong đó giây không có sẵn và bạn cố gắng trích xuất giây bằng cách sử dụng thành phần nó sẽ trả về bạn '0' trong trường hợp đó. –

+0

Tôi đã tìm thấy sự cố của mình ... Tôi đã tuyên bố cấu trúc có tên là 'Lịch' đã can thiệp vào 'Calendar.Component' rõ ràng. – netigger

8

Trong Swift 3 bạn có thể làm điều này,

let date = Date() 
let hour = Calendar.current.component(.hour, from: date) 
17
let date = Date()  
let units: Set<Calendar.Component> = [.hour, .day, .month, .year] 
let comps = Calendar.current.dateComponents(units, from: date) 
2
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) 
0
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 
Các vấn đề liên quan