import Foundation

extension Date {
	/*
    getTime은 Date 타입
	let date: String = Date().getTime("yyyy.MM.dd HH:mm:ss", time: getTime)
    */
    func getTime(_ dataFormat: String, time: Date) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = dataFormat
        return dateFormatter.string(from: time)
    }
    
    func getDateFormatter(formatt:String = "yyyy-MM-dd hh:mm:ss") -> String{
        
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "ko_KR")
        formatter.dateFormat = formatt
        
        return formatter.string(from: self)
    }
    
    func getDateWithKoreanAMPM() -> String{
        
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "ko_KR")
        formatter.dateFormat = "HH"
        
        var ampm = "오전 "
        if let hour = Int(formatter.string(from: self)){
            if hour > 11 {
                ampm = "오후 "
            }
        }
        formatter.dateFormat = "hh:mm"
        
        return  ampm + formatter.string(from: self)
    }
    func getDateWithEngAMPM() -> String{
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "ko_KR")
        formatter.dateFormat = "HH"
        
        var ampm = "AM"
        if let hour = Int(formatter.string(from: self)){
            if hour > 11 {
                ampm = "PM"
            }
        }
        formatter.dateFormat = "hh:mm"
        
        return  formatter.string(from: self) + ampm
    }
    
    //두 날짜간의 차이를 반환
    func getDateDiff(day1 : String, day2:String) -> Int{
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier:"ko_KR")
        formatter.dateFormat = "yyyy-MM-dd"
        
        let startDate = formatter.date(from:day1)!
        let endDate = formatter.date(from:day2)!
        
        let interval = endDate.timeIntervalSince(startDate)
        let days = Int(interval / 86400)
        
        return days
    }
    
    static func getDateDiffOnlyDay(day1:String, day2:String) -> Int{
        if let date1 = Date.getDateToStringConvert(strDate: day1)
            , let date2 = Date.getDateToStringConvert(strDate: day2){
            let convertDate1 = date1.getDateFormatter(formatt: "yyyyMMdd")
            let convertDate2 = date2.getDateFormatter(formatt: "yyyyMMdd")
            
            if let dateonlyDay1 = Date.getDateToStringConvert(strDate: convertDate1), let dateonlyDay2 = Date.getDateToStringConvert(strDate: convertDate2){
                return dateonlyDay2.getDateDiffDay(date2: dateonlyDay1)
            }
        }
        
        return 0
    }
    
    static func getDateDiffOnlyMin(day1:String, day2:String) -> Bool {
        
        if let date1 = Date.getDateToStringConvert(strDate: day1), let date2 = Date.getDateToStringConvert(strDate: day2) {

            let convertDate1 = date1.getDateFormatter(formatt: "yyyyMMddHHmm")
            let convertDate2 = date2.getDateFormatter(formatt: "yyyyMMddHHmm")
            
            return convertDate1 == convertDate2
        }
        
        return false
    }
    
    static func getDateDiff(day:String) -> Int{
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier:"ko_KR")
        formatter.dateFormat = "yyyy-MM-dd"
        
        let currentDate = Date()
        let saveDate = formatter.date(from: day)!
        
        let interval = currentDate.timeIntervalSince(saveDate)
        let days = Int(interval / 86400)
        
        return days
    }
    
    ///두 날짜간의 날짜 차이를 반환
    func getDateDiffDay(date2:Date) -> Int{
        let interval = date2.timeIntervalSince(self)
        let days = Int(interval / 86400)
        return days
    }
    
    ///두 날짜간의 초 차이를 반환
    func getDateDiffSecond(date2:Date) -> Int{
        let interval = date2.timeIntervalSince(self)
        let second = Int(interval)
        return second
    }
    
    ///두 날짜간의 밀리초 차이를 반환
    func getDateDiffMilliSecond(date2:Date) -> Double{
        let interval = date2.timeIntervalSince(self)
//        let milliSecond = Int(interval * 1000)
        
        return Double(interval)
    }
    
    func getHMSString() -> String{
        var strLine02 = ""
        let diffSecond = self.getDateDiffSecond(date2: Date())
        if diffSecond < 60 {
            strLine02 = "(\(diffSecond.getString())\("seconds.ago".localized))"
        }else if diffSecond >= 60 && diffSecond < 3600{
            let minute = diffSecond / 60
            strLine02 = "(\(minute.getString())\("minutes.ago".localized))"
        }else if diffSecond >= 3600 && diffSecond < 86400{
            let hour = diffSecond / 3600
            strLine02 = "(\(hour.getString())\("hours.ago".localized))"
        }else{
            let diffDay = self.getDateDiffDay(date2: Date())
            strLine02 = "(\(diffDay.getString())\("days.ago".localized))"
        }
        return strLine02
    }
    
    static func getHMSType(time:Int) -> String{
        if time < 60{
            return String(format: "00:%02d", time)
        }
        else if time >= 60 && time < 3600{
            return String(format: "%02d:%02d", time / 60, (time % 60))
        }
        else if time > 3600 {
            return String(format: "%02d:%02d:%02d", time / 3600, (time % 3600) / 60 , (time % 3600) % 60 )
        }
        return "00:00"
    }
    
    static func getDateToStringConvert(strDate:String) -> Date?{
        let components = strDate.components(separatedBy: ".")
        if components.count > 0 , let strDate = components.first{
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
            
            if let date = dateFormatter.date(from: strDate)
            {
                return date
            }
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            if let date = dateFormatter.date(from: strDate)
            {
                return date
            }
            
            if let date = ISO8601DateFormatter().date(from: strDate){
                return date
            }
        }
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
        
        if let date = dateFormatter.date(from: strDate)
        {
            return date
        }
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
        if let date = dateFormatter.date(from: strDate)
        {
            return date
        }
        dateFormatter.dateFormat = "yyyyMMddHHmmss"
        if let date = dateFormatter.date(from:strDate){
            return date
        }
        dateFormatter.dateFormat = "yyyyMMdd"
        if let date = dateFormatter.date(from:strDate){
            return date
        }
        
        if let date = ISO8601DateFormatter().date(from: strDate){
            return date
        }
        return nil
    }

    static func getDateOnlyYYYYMMdd(strDate:String) -> Date?{
        if let convert = Date.getDateToStringConvert(strDate: strDate)?.getDateFormatter(formatt: "yyyMMdd"){
            return Date.getDateToStringConvert(strDate: convert)
        }
        return Date()
    }
    
    static func getDateConvertUnixTime(time:Double) -> Date?{
        //1581406721153
        let timeInterval = time / 1000
        let expireDate = Date(timeIntervalSince1970: timeInterval)
//        expireDate.addTimeInterval(time.truncatingRemainder(dividingBy: 1000))
        
        let koreaString = expireDate.getDateFormatter(formatt: "yyyy-MM-dd HH:mm:ss")
//        Singletone.dashLog(self, "time = \(time) string = \(koreaString)")
        if let date = Date.getDateToStringConvert(strDate: koreaString){
            return date
        }
        return nil
    }
    
    func getDayofWeek() -> String{
        let weekDay = Calendar.current.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:
            break
        }
        return ""
    }
}

 

'Swift > Extention' 카테고리의 다른 글

[SWIFT]UIImage Extension  (0) 2023.05.08
[SWIFT]Array Extension  (0) 2023.05.08
[SWIFT]Dictionary Extension  (0) 2023.05.08
[SWIFT]NSMutableAttributedString Extention  (0) 2023.04.24
[SWIFT]String Extension 유용한 기능  (0) 2023.04.24

+ Recent posts