extension Date {
static func stringFromDateWithFormat(date: Date, format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
return dateFormatter.string(from: date as Date)
}
static func getCurrentLocalDate() -> Date {
var now = Date()
let component = Date().createComponent()
let calendar = Calendar.current
now = calendar.date(from: component)!
return now as Date
}
static func dateFromString(dateString: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
return dateFormatter.date(from: dateString)!
}
static func getCurrentLocalDateString() -> String {
let component = Date().createComponent()
let dateString = "\(String(component.month!))-\(String(component.day!))-\(String(component.year!))@\(String(component.hour!)).\(String(component.minute!)).\(String(component.second!))"
return dateString
}
func createComponent() -> DateComponents {
let now = Date()
var nowComponent = DateComponents()
nowComponent.year = Calendar.current.component(.year, from: now)
nowComponent.month = Calendar.current.component(.month, from: now)
nowComponent.day = Calendar.current.component(.day, from: now)
nowComponent.hour = Calendar.current.component(.hour, from: now)
nowComponent.minute = Calendar.current.component(.minute, from: now)
nowComponent.second = Calendar.current.component(.second, from: now)
nowComponent.timeZone = TimeZone(abbreviation: "GMT")!
return nowComponent
}
}