前端之家收集整理的这篇文章主要介绍了
swift - DateUtil,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class DateUtil: NSObject {
class func dateFormatterStrByM(dateStr:String)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy-MM-dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
let showFormatter:NSDateFormatter=NSDateFormatter()
showFormatter.dateFormat="MM月dd日 HH:mm"
return showFormatter.stringFromDate(date)
}
class func dateFormatterStrByS(dateStr:String)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy-MM-dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
let showFormatter:NSDateFormatter=NSDateFormatter()
showFormatter.dateFormat="yyyy-MM-dd"
return showFormatter.stringFromDate(date)
}
class func dateFormatterStr(dateStr:String)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
let showFormatter:NSDateFormatter=NSDateFormatter()
showFormatter.dateFormat="yyyy-MM-dd HH:mm"
return showFormatter.stringFromDate(date)
}
class func dateFormatterDay(dateStr:String)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
let showFormatter:NSDateFormatter=NSDateFormatter()
showFormatter.dateFormat="yyyy-MM-dd"
return showFormatter.stringFromDate(date)
}
class func dateFormatterhms(dateStr:String)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
let showFormatter:NSDateFormatter=NSDateFormatter()
showFormatter.dateFormat="HH:mm:ss"
return showFormatter.stringFromDate(date)
}
class func restoreTimeFullString(date:NSTimeInterval)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
let pointDate:NSDate=NSDate(timeIntervalSince1970: date)
return dateFormatter.stringFromDate(pointDate)
}
class func minuteFullString(date:Double)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy-MM-dd HH:mm"
let pointDate:NSDate=NSDate(timeIntervalSince1970: NSTimeInterval(date))
return dateFormatter.stringFromDate(pointDate)
}
class func jieMinuteFullString(date:Double)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="MM月dd日 HH:mm"
let pointDate:NSDate=NSDate(timeIntervalSince1970: NSTimeInterval(date))
return dateFormatter.stringFromDate(pointDate)
}
class func dateTimestampExact(dateStr:String)->Double{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
let date:NSDate=dateFormatter.dateFromString(dateStr)!
return date.timeIntervalSince1970
}
class func hourString(date:Double)->String{
let dateFormatter:NSDateFormatter=NSDateFormatter()
dateFormatter.dateFormat="HH:mm"
let pointDate:NSDate=NSDate(timeIntervalSince1970: NSTimeInterval(date))
return dateFormatter.stringFromDate(pointDate)
}
class func getDescTime(date:Double,sysDate:Double)->String{
var descTime:String=""
var hourTime:String=""
if date>0{
let d1_time:Double=round(date * 1000)
let d2_time:Double=round(sysDate * 1000)
let days:Int=Int(daysBetween(d2_time,bdate: d1_time))
hourTime=hourString(date)
if days<0 {
let daysAfter:Int=abs(days)
if(daysAfter==0){
descTime="今天"
}else{
return jieMinuteFullString(date)
}
return "\(descTime) \(hourTime)"
}
if(days==0){
descTime="今天"
}else if(days==1){
descTime="昨天"
}else if(days==2){
descTime="前天"
}else if(days>2 && days<7){
descTime="\(days)天前"
}else if(days>=7 && days<=30){
descTime="一周前"
}else if(days>=30 && days<=182){
descTime="一月前"
}else if(days>=182 && days<=365){
descTime="半年前"
}else if(days>=365){
descTime="一年前"
}else{
return minuteFullString(date)
}
return "\(descTime) \(hourTime)"
}else{
return minuteFullString(date)
}
}
class func countDown(date:Double,ntimes:Double,timeOver:()->Void)->String{
var teimsdescs:String=""
if date>0{
let d1_time:Double=date*Double(1000)
let d2_teim:Double=ntimes*Double(1000)
let diff:Int64 = Int64(round(d1_time-d2_teim))
if diff>0{
let day:Int64=diff/(24*60*60*1000)
let hour:Int64=diff/(60*60*1000)-(day*24)
let min:Int64=((diff / (60*1000))-(day*24*60)-(hour*60))
let s:Int64=diff / 1000 - (day*24*60*60)-(hour*60*60)-(min*60)
teimsdescs="\(day)天\(hour)时\(min)分\(s)秒"
}else{
timeOver()
}
}
return teimsdescs
}
class func daysBetween(smdate:Double,bdate:Double)->Double{
return (smdate/Double(86400000))-(bdate/Double(86400000))
}
class func diffDay(preTime:Double,nowTime:Double)->Double{
return abs((nowTime - preTime) / 1000 / 3600 / 24)
}
}
原文链接:https://www.f2er.com/swift/324256.html