//
//  DateConversion.swift
//  Zockerhoehle
//
//  Created by Julian-Steffen Müller on 24.09.19.
//  Copyright © 2019 Julian-Steffen Müller. All rights reserved.
//

import Foundation

extension Date {
    static let zockerhoehle_date_format = "yyyy-MM-dd'T'HH:mm:ssZ"
    func formattedInTimeZone(timezone : TimeZone = .current) -> String {
        // 1) Create a DateFormatter() object.
        let format = DateFormatter()
        
        // 2) Set the current timezone to .current, or America/Chicago.
        format.timeZone = timezone
         
        // 3) Set the format of the altered date.
        format.dateFormat = Date.zockerhoehle_date_format
         
        // 4) Set the current date, altered by timezone.
        let dateString = format.string(from: self)
        
        return dateString
    }
    
    static func from(string : String, timezone : TimeZone = .current) -> Date? {
        let format = DateFormatter()
        format.timeZone = timezone
        format.dateFormat = Date.zockerhoehle_date_format
        
        return format.date(from: string)
    }
}
