//
//  Date+Extensions.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 21/02/2023.
//

import Foundation

extension Date {
    
    static let monthYearFormatter = {
        let df = DateFormatter()
        df.dateFormat = "MMMM yyyy"
        return df
    }()
    
    static let dayFormatter = {
        let df = DateFormatter()
        df.dateStyle = .short
        df.timeStyle = .none
        return df
    }()
    
    var startOfDay: Date {
        return Calendar.current.startOfDay(for: self)
    }
    
    func get(_ component: Calendar.Component, calendar: Calendar = Calendar.current) -> Int {
        return calendar.component(component, from: self)
    }
    
    var year: Int { self.get(.year) }
    var month: Int { self.get(.month) }
    
    var formattedYear: String { return "\(self.year)" }
    var formattedMonth: String { return Date.monthYearFormatter.string(from: self) }
    var formattedDay: String { return Date.dayFormatter.string(from: self) }
    
}
