Date相關轉(zhuǎn)換參考
1. 根據(jù)時間戳計算某個時間段的時間 (時間Date類型) [timeInterval 以‘秒 s’單位 例如 一天后 60*60*24]
?static?func?calculationDateFromTimeStamp(timeStamp:String,
timeInterval:TimeInterval) ->Date{
? ? ? ??//根據(jù)時間戳轉(zhuǎn)化Date
? ? ? ? let?interval:TimeInterval=TimeInterval.init(timeStamp)!
? ? ? ? let?date = Date(timeIntervalSince1970: interval)
? ? ? ??return??date.addingTimeInterval(timeInterval)
? ? }
2.根據(jù)時間戳與當前時間的比較
?static?func?compareCurrntTime(timeStamp:String) ->String{
? ? ? ??//計算出時間戳距離現(xiàn)在時間的一個秒數(shù)(..s)
? ? ? ? let?interval:TimeInterval=TimeInterval.init(timeStamp)!
? ? ? ? let?date =Date(timeIntervalSince1970: interval)
? ? ? ? var?timeInterval = date.timeIntervalSinceNow
? ? ? ??//得到的是一個負值 (加' - ' 得正以便后面計算)
? ? ? ? timeInterval = -timeInterval
? ? ? ??//根據(jù)時間差 做所對應的文字描述 (作為返回文字描述)
? ? ? ? var?result:String
? ? ? ?//一分鐘以內(nèi)
? ? ? ??if?interval <60{
? ? ? ? ? ? result ="剛剛"
? ? ? ? ? ? return?result
? ? ? ? }else?if??Int(timeInterval/60) <60{
? ? ? ? ? ?//一小時以內(nèi)
? ? ? ? ? ? result =String.init(format:"%@分鐘前",String(Int(timeInterval/60)))
? ? ? ? ? ? return?result
? ? ? ? }else?if?Int((timeInterval/60)/60) <24{
? ? ? ? ? ? //一天以內(nèi)
? ? ? ? ? ? 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.比較兩個時間的大小 (一般用作設置活動開始時間/結束時間) (根據(jù)bool類型判斷)
static?func?compareTimeOfSize(startDate:Date, endDate:Date? =nil) ->Bool{
? ? ? ??//當前時間
? ? ? ??let?nowDate =Date.init()
? ? ? ??let calendar:Calendar = Calendar.current
? ? ? ? ?let?unit:Set = [.day,.hour]
? ? ? ??//開始時間不能小于當前時間 B衬蟆J琛哈垢!
? ? ? ? let??commponentTemp:DateComponents= calendar.dateComponents(unit, from: nowDate, to: startDate)
? ? ? ??guard?commponentTemp.day! >0|| commponentTemp.hour! >0else{
? ? ? ? ? ? print("開始時間不能小于當前時間!C适洹!")
? ? ? ? ? ? return?false
? ? ? ? }
? ? ? ??//判斷結束時間是否為空
? ? ? ? if?endDate !=nil? {
? ? ? ? ? ??let??commponent:DateComponents = calendar.dateComponents(unit, from: startDate, to: endDate!)
? ? ? ? ? ??guard?commponent.day! >0|| commponent.hour! >0?else{
? ? ? ? ? ? ? ? print("結束時間不能小于開始時間!2匕摹!")
? ? ? ? ? ? ? ? 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?"訂單/活動已結束O裳痢4酥蕖!"
? ? ? ? }
? ? ? ??//- 天
? ? ? ? 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)
? ? }