반응형
NSDate를 사용하여 Swift 3에서 시간 (시, 분, 초)을 얻는 방법은 무엇입니까?
Swift 3의 NSDate 클래스에서 시간, 분, 초를 어떻게 결정할 수 있습니까?
Swift 2 :
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
스위프트 3?
에서 스위프트 3.0 애플은 'NS'접두사와 만든 모든 것을 간단하게 제거. 아래는 'Date'클래스 (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)")
스위프트 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)")
let date = Date()
let units: Set<Calendar.Component> = [.hour, .day, .month, .year]
let comps = Calendar.current.dateComponents(units, from: date)
스위프트 4
let calendar = Calendar.current
let time=calendar.dateComponents([.hour,.minute,.second], from: Date())
print("\(time.hour!):\(time.minute!):\(time.second!)")
Swift 3에서는 이렇게 할 수 있습니다.
let date = Date()
let hour = Calendar.current.component(.hour, from: date)
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)
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
가장 유용하게 사용할 수 있도록 다음 함수를 만듭니다.
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
그리고 그게 다야...
이것은 둘 이상의 수업에서 현재 날짜를 사용하려는 사람들에게 유용 할 수 있습니다.
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
반응형
'program story' 카테고리의 다른 글
스택 네비게이터에서 헤더 숨기기 React Navigation (0) | 2020.11.09 |
---|---|
부트 스트랩 드롭 다운이 작동하지 않음 (0) | 2020.11.09 |
Python이 Java / C #보다 느립니까? (0) | 2020.11.09 |
DataTable에서 특정 행 삭제 (0) | 2020.11.09 |
WPF-표준 단추를 사용하여 위쪽 및 아래쪽 화살표가있는 단추 만들기 (0) | 2020.11.09 |