Swift 一些时间处理
2018-08-25 本文已影响139人
izsm
1. 根据一个时间字符串转换任意样式
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. 获取天干地支
定义天干地支数组
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. 获取农历
定义农历数据
private let months: [String] = ["正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
"九月", "十月", "冬月", "腊月"]
private let days: [String] = [ "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿",
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"]
给定日期计算农历
extension Date {
// 获取农历
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()) // "七月 十五"