import Foundation

extension Date {
    private static let apiFormatter: DateFormatter = {
        let f = DateFormatter()
        f.dateFormat = "yyyy-MM-dd"
        f.locale = Locale(identifier: "en_US_POSIX")
        return f
    }()

    private static let displayFormatter: DateFormatter = {
        let f = DateFormatter()
        f.dateFormat = "EEE, MMM d"
        return f
    }()

    var apiDateString: String {
        Self.apiFormatter.string(from: self)
    }

    var displayString: String {
        Self.displayFormatter.string(from: self)
    }

    static func fromAPI(_ string: String) -> Date? {
        apiFormatter.date(from: string)
    }
}
