Swift4, 日期格式(yyyy-MM-dd)轉為民國年月日格式

  • 1098
  • 0

Swift4, 日期格式(yyyy-MM-dd)轉為民國年月日格式

func TransToTWYear(str:String)->String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy/MM/dd"
        let newDate = dateFormatter.date(from:str)
        var calendar = Calendar.current
        calendar = .current
        let year = calendar.component(.year, from:newDate!)
        let month = calendar.component(.month, from:newDate!)
        let day = calendar.component(.day, from:newDate!)
        var newMonth = "", newDay = "", newYear = ""
        if (month < 10) {
            newMonth = "0" + String(month)
        } else {
            newMonth = String(month)
        }
        if (day < 10) {
            newDay = "0" + String(day)
        } else {
            newDay = String(day)
        }
        newYear = String(year - 1911)
        let strDate = newYear + "年" + newMonth + "月" + newDay + "日"
        return strDate
    }