1. 根據(jù)一個時間字符串轉(zhuǎn)換任意樣式
extension String {
func formatDate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let formatDate = dateFormatter.date(from: self) else { return "" }
let calendar = Calendar.current
_ = calendar.component(.era, from: formatDate)
let year = calendar.component(.year, from: formatDate)
let month = calendar.component(.month, from: formatDate)
let day = calendar.component(.day, from: formatDate)
let hour = calendar.component(.hour, from: formatDate)
let minute = calendar.component(.minute, from: formatDate)
let second = calendar.component(.second, from: formatDate)
return String(format: "%.2zd年%.2zd月%.2zd日 %.2zd時:%.2zd分:%.2zd秒", year, month, day, hour, minute, second)
}
}
案列:
let dateString = "2018-08-25 20:00:00"
debugPrint(dateString.formatDate()) // "2018年08月25日 20時:00分:00秒"
2. 獲取星期幾
extension String {
func featureWeekday() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let formatDate = dateFormatter.date(from: self) else { return "" }
let calendar = Calendar.current
let weekDay = calendar.component(.weekday, from: formatDate)
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 ""
}
}
}
案列
let dateString = "2018-08-25 20:00:00"
debugPrint(dateString.featureWeekday()) // "星期六"
3. 獲取生肖年
先定義一組生肖:
private let zodiacs: [String] = ["鼠年", "牛年", "虎年", "兔年", "龍年", "蛇年", "馬年", "羊年", "猴年", "雞年", "狗年", "豬年"]
extension Date {
// 生肖
func zodiac() -> String {
let calendar: Calendar = Calendar(identifier: .chinese)
return zodiac(year: calendar.component(.year, from: self))
}
private func zodiac(year: Int) -> String {
let zodiacIndex: Int = (year - 1) % zodiacs.count
return zodiacs[zodiacIndex]
}
}
案列
debugPrint(Date().zodiac()) // "狗年"
4. 獲取天干地支
定義天干地支數(shù)組
private let heavenlyStems: [String] = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
private let earthlyBranches: [String] = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
計算天干地支
extension Date {
// 天干地支
func era() -> String {
let calendar: Calendar = Calendar(identifier: .chinese)
return era(year: calendar.component(.year, from: self))
}
private func era(year: Int) -> String {
let heavenlyStemIndex: Int = (year - 1) % heavenlyStems.count
let heavenlyStem: String = heavenlyStems[heavenlyStemIndex]
let earthlyBrancheIndex: Int = (year - 1) % earthlyBranches.count
let earthlyBranche: String = earthlyBranches[earthlyBrancheIndex]
return heavenlyStem + earthlyBranche
}
}
案列
debugPrint(Date().era()) // "戊戌"
5. 獲取農(nóng)歷
定義農(nóng)歷數(shù)據(jù)
private let months: [String] = ["正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
"九月", "十月", "冬月", "臘月"]
private let days: [String] = [ "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"]
給定日期計算農(nóng)歷
extension Date {
// 獲取農(nóng)歷
func lunar() -> String {
let calendar: Calendar = Calendar(identifier: .chinese)
let com = calendar.dateComponents(component(), from: self)
let m_str = months[com.month! - 1]
let d_str = days[com.day! - 1]
return m_str + " " + d_str
}
private func component() -> Set<Calendar.Component> {
return [.year, .month, .day, .hour, .minute, .second]
}
}
案列
debugPrint(Date().lunar()) // "七月 十五"