Skip to content

Instantly share code, notes, and snippets.

@fbukevin
Created December 28, 2016 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbukevin/646da5db139664b172fad788c5a49403 to your computer and use it in GitHub Desktop.
Save fbukevin/646da5db139664b172fad788c5a49403 to your computer and use it in GitHub Desktop.
import Foundation
class TimeTool {
func getCurrentTimeString() -> String {
let now = Date()
let df = DateFormatter()
df.dateFormat = "YYYY/MM/dd HH:mm:ss"
return df.string(from: now)
}
func dateToString(date: Date) -> String {
let cal = Calendar.current
let yr = cal.component(.year, from: date)
let mth = cal.component(.month, from: date)
let day = cal.component(.day, from: date)
let hr = cal.component(.hour, from: date)
let min = cal.component(.minute, from: date)
let sec = cal.component(.second, from: date)
let dateString = "\(yr)/\(mth)/\(day) \(hr):\(min):\(sec)"
return dateString
}
func stringToDate(date: String) -> Date {
let df = DateFormatter()
df.dateFormat = "YYYY/MM/dd HH:mm:ss"
return df.date(from: date)!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment