Goal/Problem
I want to convert a Date
into a String
and back to a Date
. I am able to do this but I am losing precision on the way. How can I make sure that not a single bit gets lost in the process?
1573827905079978
vs 157382790508
Main code
var now = Date()
var now_as_string = Date.dateAsString(style: .dayMonthYearHourMinuteSecondMillisecondTimezone, date: now)
var back_as_date = Date.stringAsDate(style: .dayMonthYearHourMinuteSecondMillisecondTimezone, string: now_as_string)
print(Date.dateAsTimeIntervalSince1970WithoutDots(date: now))
print(Date.dateAsTimeIntervalSince1970WithoutDots(date: back_as_date))
Output
1573827905079978
157382790508
Date Extension (the place where the real magic happens)
import Foundation
extension Date {
enum Style {
case dayMonthYear
case dayMonthYearHourMinute
case dayMonthYearHourMinuteSecondMillisecondTimezone
}
static func dateAsString(style: Date.Style, date: Date) -> String{
let formatter = DateFormatter()
formatter.dateFormat = fromStyleToString(style: style)
return formatter.string(from: date)
}
private static func fromStyleToString(style: Date.Style) -> String{
switch style {
case .dayMonthYear:
return "dd.MM.yyyy"
case .dayMonthYearHourMinute:
return "dd.MM.yyyy HH:mm"
case .dayMonthYearHourMinuteSecondMillisecondTimezone:
return "dd.MM.yyyy HH:mm:ss:SSS Z"
}
}
static func stringAsDate(style: Date.Style, string: String) -> Date{
let formatter = DateFormatter()
formatter.dateFormat = fromStyleToString(style: style)
return formatter.date(from: string)!
}
static func dateAsTimeIntervalSince1970WithoutDots(date: Date) -> String{
return String(date.timeIntervalSince1970).replacingOccurrences(of: ".", with: "")
}
}