Skip to content

Instantly share code, notes, and snippets.

@erdemarslan
Created September 10, 2018 21:38
Show Gist options
  • Select an option

  • Save erdemarslan/22b2f137613be1ba7d60d606f572818a to your computer and use it in GitHub Desktop.

Select an option

Save erdemarslan/22b2f137613be1ba7d60d606f572818a to your computer and use it in GitHub Desktop.
Date ile ilgili kullanışlı extensionlarım
extension Date {
func hour() -> Int {
//Get Hour
let calendar = Calendar.current
let components = calendar.component(.hour, from: self as Date)
let hour = components.hashValue
//Return Hour
return hour
}
func minute() -> Int {
//Get Minute
let calendar = Calendar.current
let components = calendar.component(.minute, from: self as Date)
let minute = components.hashValue
//Return Minute
return minute
}
func toShortTimeString() -> String {
//Get Short Time String
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateFormat = "HH:mm"
let timeString = formatter.string(from: self as Date)
//Return Short Time String
return timeString
}
func toShortDateString() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "YYYY/MM/dd"
let dateString = formatter.string(from: self as Date)
return dateString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment