Wie können Sie die Stunde, Minute und Sekunde aus der NSDate-Klasse in Swift 3 bestimmen?
In Swift 2:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
Swift 3?
Antworten:
In Swift 3.0 entfernte Apple das Präfix 'NS' und machte alles einfach. Im Folgenden finden Sie die Möglichkeit, Stunde, Minute und Sekunde aus der 'Date'-Klasse zu erhalten (NSDate-Alternative).
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)")
Wie diese können Sie Ära, Jahr, Monat, Datum usw. erhalten, indem Sie entsprechend übergeben.
quelle
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)")
quelle
'Component' is not a member type of 'Calendar'
online bin? Lassen Sie unitFlags ... ``Calendar.Component
? Angenommen, Ihr Datum folgt "02/12/15, 16:46", wo keine Sekunden verfügbar sind, und Sie versuchen, Sekunden mithilfe von Komponenten zu extrahieren.0
In diesem Fall werden Sie zurückgegeben.Calendar
dieCalendar.Component
offensichtlich störte .let date = Date() let units: Set<Calendar.Component> = [.hour, .day, .month, .year] let comps = Calendar.current.dateComponents(units, from: date)
quelle
Swift 4
let calendar = Calendar.current let time=calendar.dateComponents([.hour,.minute,.second], from: Date()) print("\(time.hour!):\(time.minute!):\(time.second!)")
quelle
In Swift 3 können Sie dies tun,
let date = Date() let hour = Calendar.current.component(.hour, from: date)
quelle
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)
quelle
Swift 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) } }
Verwendung:
print(Date().get(.year)) // => 2020 print(Date().get(.month)) // => 08 print(Date().get(.day)) // => 18
quelle
Für den besten Nutzen erstelle ich diese Funktion:
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)" }
Sie nennen es einfach, wo immer Sie wollen:
print("Date = \(self.dateFormatting())")
Dies ist die Ausgabe:
Date = Monday 15 October 2018 - 17:26:29
wenn du nur die zeit ändern willst einfach ändern:
dateFormatter.dateFormat = "HH:mm:ss"
und das ist die Ausgabe:
Date = 17:27:30
und das ist es...
quelle
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
quelle
Dies kann nützlich sein für diejenigen, die das aktuelle Datum in mehr als einer Klasse verwenden möchten.
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 } }
Verwendung
var time = "" time = time.getCurrentTime() print(time) // 1900-12-09-12-59
quelle