Date相关转换参考
1. 根据时间戳计算某个时间段的时间 (时间Date类型) [timeInterval 以‘秒 s’单位 例如 一天后 60*60*24]
static func calculationDateFromTimeStamp(timeStamp:String,
timeInterval:TimeInterval) ->Date{
//根据时间戳转化Date
let interval:TimeInterval=TimeInterval.init(timeStamp)!
let date = Date(timeIntervalSince1970: interval)
return date.addingTimeInterval(timeInterval)
}
2.根据时间戳与当前时间的比较
static func compareCurrntTime(timeStamp:String) ->String{
//计算出时间戳距离现在时间的一个秒数(..s)
let interval:TimeInterval=TimeInterval.init(timeStamp)!
let date =Date(timeIntervalSince1970: interval)
var timeInterval = date.timeIntervalSinceNow
//得到的是一个负值 (加' - ' 得正以便后面计算)
timeInterval = -timeInterval
//根据时间差 做所对应的文字描述 (作为返回文字描述)
var result:String
//一分钟以内
if interval <60{
result ="刚刚"
return result
}else if Int(timeInterval/60) <60{
//一小时以内
result =String.init(format:"%@分钟前",String(Int(timeInterval/60)))
return result
}else if Int((timeInterval/60)/60) <24{
//一天以内
result =String.init(format:"%@小时前",String(Int((timeInterval/60)/60)))
return result
}else{
//超过一天的
let dateformatter =DateFormatter()
//自定义日期格式
dateformatter.dateFormat="yyyy年MM月dd日 HH:mm"
result = dateformatter.string(from: date as Date)
return result
}
}
3.比较两个时间的大小 (一般用作设置活动开始时间/结束时间) (根据bool类型判断)
static func compareTimeOfSize(startDate:Date, endDate:Date? =nil) ->Bool{
//当前时间
let nowDate =Date.init()
let calendar:Calendar = Calendar.current
let unit:Set = [.day,.hour]
//开始时间不能小于当前时间 !!!
let commponentTemp:DateComponents= calendar.dateComponents(unit, from: nowDate, to: startDate)
guard commponentTemp.day! >0|| commponentTemp.hour! >0else{
print("开始时间不能小于当前时间!!!")
return false
}
//判断结束时间是否为空
if endDate !=nil {
let commponent:DateComponents = calendar.dateComponents(unit, from: startDate, to: endDate!)
guard commponent.day! >0|| commponent.hour! >0 else{
print("结束时间不能小于开始时间!!!")
return false
}
}
return true
}
4.比较两个时间的大小 (一般用作活动\订单等 倒计时显示文字处理)
static func compareCurrentTimeOfSize(endDate:Date) ->String{
//当前时间
let nowDate =Date.init()
let calendar:Calendar = Calendar.current
let unit:Set = [.day, .hour, .minute, .second]
let commponent:DateComponents= calendar.dateComponents(unit, from: nowDate, to: endDate)
//判断活动倒计时是否已结束
guard commponent.day! >0|| commponent.hour! >0|| commponent.minute! >0|| commponent.second! >0 else{
return "订单/活动已结束!!!"
}
//- 天
var dStr:String!
if commponent.day! <10{
dStr =String.init(format:"%@天","0\( commponent.day ! )")
}else{
dStr =String.init(format:"%@天","\( commponent.day ! )")
}
//- 时
var hStr:String!
if commponent.hour! <10{
hStr =String.init(format:"%@时","0\( commponent.hour ! )")
}else{
hStr =String.init(format:"%@时","\( commponent.hour ! )")
}
//- 分
var mStr:String!
if commponent.minute! <10{
mStr =String.init(format:"%@分","0\( commponent.minute ! )")
}else{
mStr =String.init(format:"%@分","\( commponent.minute ! )")
}
//- 秒
var sStr:String!
if commponent.second! <10{
sStr =String.init(format:"%@秒","0\( commponent.second ! )")
}else{
sStr =String.init(format:"%@秒","\( commponent.second ! )")
}
return String.init(format:"%@%@%@%@", dStr, hStr, mStr, sStr)
}