时间转换经常用到,如下是累积的一些方法:
1. 基本转换方法
extension Date {
// 毫秒级
static func timeIntervalToDateString(msec interval: TimeInterval, _ format: String) -> String {
guard interval > 0 else {return ""}
let timeInterval: TimeInterval = interval / 1000
let date: Date = Date(timeIntervalSince1970: timeInterval)
return date.dateString(format)
}
// 秒级
static func timeIntervalToDateString(sec interval: TimeInterval, _ format: String) -> String {
guard interval > 0 else {return ""}
let timeInterval: TimeInterval = interval
let date: Date = Date(timeIntervalSince1970: timeInterval)
return date.dateString(format)
}
// 现在
static func now(_ format: String) -> String {
return Date().dateString(format)
}
// 昨天 or 昨天相对于今天的时间
static func yesterday(_ format: String) -> String {
return dateString(before: 1, format)
}
// 今天之前某一天
static func dateString(before dayNumber: Int, _ format: String) -> String {
let interval = Date().timeIntervalSince1970
let beforeInterval = interval - TimeInterval(3600 * 24 * dayNumber)
let date = Date(timeIntervalSince1970: beforeInterval)
return date.dateString(format)
}
// 今天之后某一天
static func dateString(after dayNumber: Int, _ format: String) -> String {
let interval = Date().timeIntervalSince1970
let beforeInterval = interval + TimeInterval(3600 * 24 * dayNumber)
let date = Date(timeIntervalSince1970: beforeInterval)
return date.dateString(format)
}
// 获取指定日期所在周的任意 weekDay 的日期 //
// weekDay - 1~7 '周一'~'周日'
func dateString(_ weekDay: Int, _ format: String) -> String {
let calendar = Calendar.current
var components = calendar.dateComponents([.year, .month, .day, .weekday], from: self)
if let week_day = components.weekday, let day = components.day {
var diff = 0
if week_day == 1 { // 默认 1 是 周日
diff = weekDay - 7
}else { // 2 ~ 7 - '周一' ~ '周六'
diff = weekDay - (week_day - 1)
}
components.setValue(day + diff, for: .day)
if let date_goal: Date = calendar.date(from: components) {
return date_goal.dateString(format)
}
}
return ""
}
func dateString(_ format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
static func timeInterval(_ dateStr: String, _ format: String) -> TimeInterval {
var interval: TimeInterval = 0
if let date = date(dateStr, format) {
interval = date.timeIntervalSince1970
}
return interval
}
static func todayTimeInterval() -> TimeInterval {
var interval: TimeInterval = 0
let dateStr = Date().dateString("yyyy-MM-dd")
if let date = date(dateStr, "yyyy-MM-dd") {
interval = date.timeIntervalSince1970
}
return interval
}
static func date(_ str: String, _ format: String) -> Date? {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.date(from: str)
}
}
2. 时间比较
extension Date {
/// 小于今天
static func dateLessThanToday(_ time: TimeInterval, _ format: String) -> Bool {
let s_s: String = timeIntervalToDateString(msec: time, format)
let s: TimeInterval = timeInterval(s_s, format)
let o: TimeInterval = timeInterval(Date.now(format), format)
return s < o
}
/// 小于
static func dateLessThan(_ time: TimeInterval, _ other: TimeInterval, _ format: String) -> Bool {
let s_s: String = timeIntervalToDateString(msec: time, format)
let s: TimeInterval = timeInterval(s_s, format)
let o_s: String = timeIntervalToDateString(msec: other, format)
let o: TimeInterval = timeInterval(o_s, format)
return s < o
}
/// 大于今天
static func dateMoreThanToday(_ time: TimeInterval, _ format: String) -> Bool {
let s_s: String = timeIntervalToDateString(msec: time, format)
let s: TimeInterval = timeInterval(s_s, format)
let o: TimeInterval = timeInterval(Date.now(format), format)
return s > o
}
/// 大于
static func dateMoreThan(_ time: TimeInterval, _ other: TimeInterval, _ format: String) -> Bool {
let s_s: String = timeIntervalToDateString(msec: time, format)
let s: TimeInterval = timeInterval(s_s, format)
let o_s: String = timeIntervalToDateString(msec: other, format)
let o: TimeInterval = timeInterval(o_s, format)
return s > o
}
}
3. 时间的特殊展示
extension Date {
static func diffDateStr(_ dateStr: String, _ otherDateStr: String, _ format: String) -> String {
let one = timeInterval(dateStr, format)
let other = timeInterval(otherDateStr, format)
let diff_day = fabs(one - other) / 24 / 3600
return String(format: "%.1f天", diff_day)
}
/// 返回: *天前
static func beforeDay(_ dateStr: String, _ format: String) -> String {
let date = timeInterval(dateStr, format)
let now = Date().timeIntervalSince1970
let s_day = Int(abs(date - now))
if s_day < 60 {
return "刚刚"
}else if s_day < 3600 {
return "\(s_day / 60)分钟前"
}else if s_day < 24 * 3600 {
return "\(s_day / 3600)小时前"
}else if s_day < 30 * 24 * 3600 {
return "\(s_day / 3600 / 24)天前"
}else if s_day < 12 * 30 * 24 * 3600 {
return "\(s_day / 3600 / 24 / 30)月前"
}else {
return "\(s_day / 3600 / 24 / 30 / 12)年前"
}
}
static func smartTime(_ time: TimeInterval) -> String {
let date = Date(timeIntervalSince1970: time / 1000)
let diff_cmps = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date, to: Date())
let format = DateFormatter()
if diff_cmps.year == 0 { // 今年
if diff_cmps.month == 0, diff_cmps.day == 1 { // 昨天
format.dateFormat = "昨天 HH:mm"
return format.string(from: date)
}else if diff_cmps.month == 0, diff_cmps.day == 0 { // 今天
if let h = diff_cmps.hour, h >= 1 {
return "\(h)小时前"
}else if let m = diff_cmps.minute, m >= 1 {
return "\(m)分钟前"
}else {
return "刚刚"
}
}else { // 今年的其他日子
format.dateFormat = "MM-dd HH:mm"
return format.string(from: date)
}
}else { // 非今年
format.dateFormat = "yyyy-MM-dd HH:mm"
return format.string(from: date)
}
}
}
4. 扩展后的扩展
extension Date {
enum PartitionType: String {
case MiddleTransverseLine = "-"
case Point = "."
case ObliqueLine = "/"
case none
}
/// 今天日期 // 年月日
static func today(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return now("yyyy-MM-dd")
case .Point:
return now("yyyy.MM.dd")
case .ObliqueLine:
return now("yyyy/MM/dd")
case .none:
return now("yyyyMMdd")
}
}
/// 当前时间
static func now(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return now("yyyy-MM-dd HH:mm:ss")
case .Point:
return now("yyyy.MM.dd HH:mm:ss")
case .ObliqueLine:
return now("yyyy/MM/dd HH:mm:ss")
case .none:
return now("yyyyMMddHHmmss")
}
}
/// 昨天日期
static func yesterday(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return yesterday("yyyy-MM-dd")
case .Point:
return yesterday("yyyy.MM.dd")
case .ObliqueLine:
return yesterday("yyyy/MM/dd")
case .none:
return yesterday("yyyyMMdd")
}
}
/// 之后日期
static func after(_ dayNumber: Int, _ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return dateString(after: dayNumber, "yyyy-MM-dd")
case .Point:
return dateString(after: dayNumber, "yyyy.MM.dd")
case .ObliqueLine:
return dateString(after: dayNumber, "yyyy/MM/dd")
case .none:
return dateString(after: dayNumber, "yyyyMMdd")
}
}
/// 之前日期
static func before(_ dayNumber: Int, _ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return dateString(before: dayNumber, "yyyy-MM-dd")
case .Point:
return dateString(before: dayNumber, "yyyy.MM.dd")
case .ObliqueLine:
return dateString(before: dayNumber, "yyyy/MM/dd")
case .none:
return dateString(before: dayNumber, "yyyyMMdd")
}
}
/// 本周周一
static func firstWeekDay(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return Date().dateString(1, "yyyy-MM-dd")
case .Point:
return Date().dateString(1, "yyyy.MM.dd")
case .ObliqueLine:
return Date().dateString(1, "yyyy/MM/dd")
case .none:
return Date().dateString(1, "yyyyMMdd")
}
}
static func dateFormat(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return "yyyy-MM-dd"
case .Point:
return "yyyy.MM.dd"
case .ObliqueLine:
return "yyyy/MM/dd"
case .none:
return "yyyyMMdd"
}
}
static func timeFormat(_ type: PartitionType) -> String {
switch type {
case .MiddleTransverseLine:
return "yyyy-MM-dd HH:mm:ss"
case .Point:
return "yyyy.MM.dd HH:mm:ss"
case .ObliqueLine:
return "yyyy/MM/dd HH:mm:ss"
case .none:
return "yyyyMMddHHmmss"
}
}
static func cycleStr(_ num: Int) -> String {
switch num {
case 0:
return "周日"
case 1:
return "周一"
case 2:
return "周二"
case 3:
return "周三"
case 4:
return "周四"
case 5:
return "周五"
case 6:
return "周六"
case 7:
return "周日"
default:
return ""
}
}
static func cycleStr(_ str: String) -> String {
var string = str
string.replace("1", "周一")
string.replace("2", "周二")
string.replace("3", "周三")
string.replace("4", "周四")
string.replace("5", "周五")
string.replace("6", "周六")
string.replace("7", "周日")
return string
}
/// 一周时间排序 str: "2,4,3,1" + spaceStr: "," = "周一,周二,周三,周四"
static func cycleSortStr(_ str: String, _ spaceStr: String) -> String {
guard str.count > 0 else {return str}
let intArr = str.components(separatedBy: spaceStr).map{ Int($0) ?? 0 }.sorted(by: { $0 < $1 })
let string = intArr.map{ cycleStr($0)}.joined(separator: spaceStr)
return string
}
}
网友评论