|
|
|
@ -0,0 +1,181 @@
|
|
|
|
|
//
|
|
|
|
|
// Date+IndieMusic.swift
|
|
|
|
|
// IndieMusic
|
|
|
|
|
//
|
|
|
|
|
// Created by WenLei on 2024/1/25.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import SwiftDate
|
|
|
|
|
|
|
|
|
|
extension TimeInterval {
|
|
|
|
|
|
|
|
|
|
/// 时间戳转时间
|
|
|
|
|
func timeIntervalChangeToTimeStr(_ isSeconds: Bool = false, _ dateFormat: String? = "yyyy年MM月dd日 HH:mm") -> String {
|
|
|
|
|
//如果服务端返回的时间戳精确到毫秒,需要除以1000,否则不需要
|
|
|
|
|
if isSeconds {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let date: Date = Date.init(timeIntervalSince1970: isSeconds ? self : self/1000)
|
|
|
|
|
let formatter = DateFormatter.init()
|
|
|
|
|
formatter.dateFormat = dateFormat
|
|
|
|
|
return formatter.string(from: date as Date)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 美化时间
|
|
|
|
|
func timeString() -> String{
|
|
|
|
|
//如果服务端返回的时间戳精确到毫秒,需要除以1000,否则不需要
|
|
|
|
|
let date: Date = Date.init(timeIntervalSince1970: self/1000)
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension String {
|
|
|
|
|
/// 时间转时间戳
|
|
|
|
|
func timeStrChangeToTimeInterval(_ dateFormat: String? = "yyyy-MM-dd HH:mm") -> TimeInterval {
|
|
|
|
|
let formatter = DateFormatter.init()
|
|
|
|
|
formatter.dateFormat = dateFormat
|
|
|
|
|
let date = formatter.date(from: self)! as NSDate
|
|
|
|
|
return date.timeIntervalSince1970
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Date {
|
|
|
|
|
/// 时间美化
|
|
|
|
|
func dateString(isShowYear: Bool = true, format: String = "yyyy年MM月dd日 HH:mm") -> String {
|
|
|
|
|
|
|
|
|
|
if isToday {
|
|
|
|
|
return toFormat("今天 HH:mm")
|
|
|
|
|
} else if isYesterday {
|
|
|
|
|
return toFormat("昨天 HH:mm")
|
|
|
|
|
} else if isSameWeek() {
|
|
|
|
|
return toFormat("EEEE HH:mm")
|
|
|
|
|
} else if isSameYear() {
|
|
|
|
|
return isShowYear ? toFormat(format) : toFormat("MM月dd日 HH:mm")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toFormat(format)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 比较时间
|
|
|
|
|
func compareTime(startTime:NSString,endTime:NSString) -> ComparisonResult{
|
|
|
|
|
return startTime.compare(endTime as String)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, .month, .year], from: Date() )
|
|
|
|
|
//self
|
|
|
|
|
let selfComponents = calendar.dateComponents([.day, .month, .year], 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([.weekOfMonth,.month,.year], from: Date() )
|
|
|
|
|
//self
|
|
|
|
|
let selfComponents = calendar.dateComponents([.weekOfMonth,.month,.year], from: self as Date)
|
|
|
|
|
|
|
|
|
|
return (selfComponents.year == nowComponents.year) && (selfComponents.month == nowComponents.month) && (selfComponents.weekOfMonth == nowComponents.weekOfMonth)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isSameYear() -> Bool {
|
|
|
|
|
let calendar = Calendar.current
|
|
|
|
|
//当前时间
|
|
|
|
|
let nowComponents = calendar.dateComponents([.weekOfMonth,.month,.year], from: Date() )
|
|
|
|
|
//self
|
|
|
|
|
let selfComponents = calendar.dateComponents([.weekOfMonth,.month,.year], from: self as Date)
|
|
|
|
|
|
|
|
|
|
return (selfComponents.year == nowComponents.year)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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([.weekOfMonth], from: self as Date)
|
|
|
|
|
return weekdays.object(at: theComponents.weekOfMonth!) as! String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Int {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//extension BMKTime {
|
|
|
|
|
// func timeToToSeconds() -> Int {
|
|
|
|
|
//
|
|
|
|
|
// let daySeconds = datesToSeconds(dates: Int(self.dates))
|
|
|
|
|
// let hoursSeconds = hoursToSeconds(hours: Int(self.hours))
|
|
|
|
|
// let minutesSeconds = minutesToSeconds(minutes: Int(self.minutes))
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// return daySeconds + hoursSeconds + minutesSeconds
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 天数转秒数
|
|
|
|
|
func datesToSeconds(dates: Int) -> Int {
|
|
|
|
|
return dates * 24 * 60 * 60
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 小时转秒数
|
|
|
|
|
func hoursToSeconds(hours: Int) -> Int {
|
|
|
|
|
return hours * 60 * 60
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 分钟转秒数
|
|
|
|
|
func minutesToSeconds(minutes: Int) -> Int {
|
|
|
|
|
return minutes * 60
|
|
|
|
|
}
|