Swift 3のNSDateクラスから時、分、秒をどのように判断できますか?
スイフト2:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
スウィフト3?
Swift 3のNSDateクラスから時、分、秒をどのように判断できますか?
スイフト2:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
スウィフト3?
回答:
でスウィフト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)")
これらのように、対応するパスを渡すことで、時代、年、月、日付などを取得できます。
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)")
'Component' is not a member type of 'Calendar'
「letunitFlags ...」にオンラインになった場合
Calendar.Component
か?日付が「02/12 / 15,16:46」に続いていると考えてください。秒は使用できません0
。コンポーネントを使用して秒を抽出しようとすると、その場合に返されます。
Calendar
干渉するという名前の構造体を宣言しましたCalendar.Component
。
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)
}
}
使用法:
print(Date().get(.year)) // => 2020
print(Date().get(.month)) // => 08
print(Date().get(.day)) // => 18
最も便利なように、私はこの関数を作成します。
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
以上です...
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
これは、現在の日付を複数のクラスで使用したい場合に便利です。
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