
import Foundation

extension Date{
    var isToday:Bool{
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMdd"
        let today = formatter.string(from: Date())
        let target = formatter.string(from: self)
        if today == target{
            return true
        }
        return false
    }
    
}

extension Date{
    func fromString(format:String) -> String {
        let formmater:DateFormatter = {
            let formatter = DateFormatter()
            formatter.locale = Locale.current
            formatter.dateFormat = format
            return formatter
        }()
        return formmater.string(from: self)
    }
}

extension Date{
    func fromWeekToSting() -> String?{
        let weekday = Calendar(identifier: .gregorian).component(.weekday, from: self)
        switch weekday {
        case 1:
            return "일요일"
        case 2:
            return "월요일"
        case 3:
            return "화요일"
        case 4:
            return "수요일"
        case 5:
            return "목요일"
        case 6:
            return "금요일"
        case 7:
            return "토요일"
        default:
            return nil
        }
    }
    
    func fromWeekToInt() -> Int {
        let weekday = Calendar(identifier: .gregorian).component(.weekday, from: self)
        return weekday
    }
}

extension Date{
    static func ==(lhs: Date, rhs: Date) -> Bool {
        return lhs.fromString(format: "yyyyMMdd") == rhs.fromString(format: "yyyyMMdd")
    }
}

extension Date{
    func adding(forYear:Int) -> Date? {
        return Calendar.current.date(byAdding: .year, value: forYear, to: self)
    }
}
