How to get time (hours, minutes, seconds) in SWIFT 3?

How to determine the hours, minutes and seconds of the NSDate class in Swift 3?

In Swift 2:

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

Swift 3?

In Swift 3.0, Apple removed the “NS” prefix and made everything simple. The following It is a method to get hours, minutes and seconds from the’Date’ class (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)")

You can get these in the corresponding way Era, year, month, date, etc.

How to determine the hour, minute and second of the NSDate class in Swift 3?

In Swift 2:

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

Swift 3?

In Swift 3.0, Apple removed the “NS” prefix and made everything simple. The following is to get the hours, minutes and seconds from the’Date’ class的方法(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)")

Like these, you can get the era, year, month, date, etc. in a corresponding way.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4155 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.