extension Date {
/// 时间戳转日期
///
/// - Parameter timeInterval: 时间戳
/// - Returns: 结果
static func timeString(timeInterval: TimeInterval) -> String{
//如果服务端返回的时间戳精确到毫秒,需要除以1000,否则不需要
let date = getNowDateFromatAnDate(Date(timeIntervalSince1970: timeInterval/1000))
let formatter = DateFormatter()
if date.isToday() {
//是今天
formatter.dateFormat = "今天HH:mm"
return formatter.string(from: date)
}else if date.isYesterday(){
//是昨天
formatter.dateFormat = "昨天HH:mm"
return formatter.string(from: date)
}else if date.isSameWeek(){
//是同一周
let week = date.weekdayStringFromDate()
formatter.dateFormat = "\(week)HH:mm"
return formatter.string(from: date)
}else{
formatter.dateFormat = "yyyy-MM-dd HH:mm"
return formatter.string(from: date)
}
}
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
}
/// 根据本地时区转换
static func getNowDateFromatAnDate(_ anyDate: Date?) -> Date {
//设置源日期时区
let sourceTimeZone = NSTimeZone(abbreviation: "UTC")
//或GMT
//设置转换后的目标日期时区
let destinationTimeZone = NSTimeZone.local as NSTimeZone
//得到源日期与世界标准时间的偏移量
var sourceGMTOffset: Int? = nil
if let aDate = anyDate {
sourceGMTOffset = sourceTimeZone?.secondsFromGMT(for: aDate)
}
//目标日期与本地时区的偏移量
var destinationGMTOffset: Int? = nil
if let aDate = anyDate {
destinationGMTOffset = destinationTimeZone.secondsFromGMT(for: aDate)
}
//得到时间偏移量的差值
let interval = TimeInterval((destinationGMTOffset ?? 0) - (sourceGMTOffset ?? 0))
//转为现在时间
var destinationDateNow: Date? = nil
if let aDate = anyDate {
destinationDateNow = Date(timeInterval: interval, since: aDate)
}
return destinationDateNow!
}
}
Swift 4.1时间戳转时间(今天、昨天、星期)+时区转换
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 2018.2.3 目标:身心健康,财富丰盛 好种子: 1早起健康早餐,种下健康的种子,回向群里姐妹及家人身心健康 ...
- 一般来说,喜欢站立的人,比喜欢躺着或或坐着的人,身材更好。与躺着的姿势相比,站姿所消耗的能量要多出10%。而单腿站...
- 学好word,才能使我们工作的时候更加专注于文档内容。 不管是大学生还是职场人士,word绝对适合你花时间去精通它...