很多项目中都需要显示时间,比如聊天界面消息日期、朋友圈的说说的日期等。本文提供一个Swift下的关于时间处理的extension,即NSData的扩展,主要实现功能:
1.将时间戳格式化输出:今天HH:mm、昨天HH:mm、星期HH:mm、年-月-日 HH:mm等方式显示
2.比较两个时间字符串的大小,也就是两个时间的早晚
3.时间字符串转换时间戳
4.时间戳转换时间字符串的简单方法
主要方法:
1.多样式格式化输出方法:
class func timeString(timeInterval:TimeInterval) -> String{
//如果服务端返回的时间戳精确到毫秒,需要除以1000,否则不需要
let date:NSDate = NSDate.init(timeIntervalSince1970: timeInterval/1000)
let formatter = DateFormatter.init()
if date.isToday() {
//是今天
formatter.dateFormat = "今天HH:mm"
return formatter.string(from: date as Date)
}else if date.isYesterday(){
//是昨天
formatter.dateFormat = "昨天HH:mm"
return formatter.string(from: date as Date)
}else if date.isSameWeek(){
//是同一周
let week = date.weekdayStringFromDate()
formatter.dateFormat = "\(week)HH:mm"
return formatter.string(from: date as Date)
}else{
formatter.dateFormat = "yyyy-MM-dd HH:mm"
return formatter.string(from: date as Date)
}
}
2.时间字符串比较早晚
class func compareTime(startTime:NSString,endTime:NSString) -> ComparisonResult{
return startTime.compare(endTime as String)
}
3.时间字符串转换时间戳:
class func timeStrChangeToTimeInterval(startTime:String,dateFormat:String?) -> TimeInterval {
let formatter = DateFormatter.init()
if dateFormat == nil {
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
formatter.dateFormat = dateFormat
}
let date = formatter.date(from: startTime)! as NSDate
return date.timeIntervalSince1970
}
4.时间戳转换时间字符串简单方法
class func timeIntervalChangeToTimeStr(timeInterval:TimeInterval,dateFormat:String?) -> String {
//如果服务端返回的时间戳精确到毫秒,需要除以1000,否则不需要
let date:NSDate = NSDate.init(timeIntervalSince1970: timeInterval/1000)
let formatter = DateFormatter.init()
if dateFormat == nil {
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
}else{
formatter.dateFormat = dateFormat
}
return formatter.string(from: date as Date)
}
以上提供的主要方法以类方法的方式提供,方便项目调用,当然也可以直接用对象方法,因为在Swift中如果方法前面不加访问限制的话,是全局的,比如在“格式化输出方法”调用的方法可以直接这样写:
//是否为今天
func isToday() -> Bool {
let calendar = Calendar.current
//当前时间
let nowComponents = calendar.dateComponents([.day,.month,.year], from: Date() )
//self
let selfComponents = calendar.dateComponents([.day,.month,.year], from: self as Date)
return (selfComponents.year == nowComponents.year) && (selfComponents.month == nowComponents.month) && (selfComponents.day == nowComponents.day)
}
//是否是昨天
func isYesterday() -> Bool {
let calendar = Calendar.current
//当前时间
let nowComponents = calendar.dateComponents([.day], from: Date() )
//self
let selfComponents = calendar.dateComponents([.day], from: self as Date)
let cmps = calendar.dateComponents([.day], from: selfComponents, to: nowComponents)
return cmps.day == 1
}
//是否同一周
func isSameWeek() -> Bool {
let calendar = Calendar.current
//当前时间
let nowComponents = calendar.dateComponents([.day,.month,.year], from: Date() )
//self
let selfComponents = calendar.dateComponents([.weekday,.month,.year], from: self as Date)
return (selfComponents.year == nowComponents.year) && (selfComponents.month == nowComponents.month) && (selfComponents.weekday == nowComponents.weekday)
//日期转换星期
func weekdayStringFromDate() -> String {
let weekdays:NSArray = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
var calendar = Calendar.init(identifier: .gregorian)
let timeZone = TimeZone.init(identifier: "Asia/Shanghai")
calendar.timeZone = timeZone!
let theComponents = calendar.dateComponents([.weekday], from: self as Date)
return weekdays.object(at: theComponents.weekday!) as! String
}
这些方法在外部可以被调用,大家可以根据项目需要修改方法访问权限:方法前用fileprivate、private、public等修饰。
方法调用:
调用就很简单啦,首先将extension拖进项目,以格式化输出时间字符串方法调用为例:
timeLabel.text = NSDate.timeString(timeInterval: "1504340026000")
ps:关于时间处理的需求千奇百怪,大家可以根据需要扩展或修改一些方法,也欢迎大家跟我交流,共同进步。