時(shí)間戳徙鱼、日期宅楞、年月日、天數(shù)的計(jì)算很常用袱吆,沒(méi)有一個(gè)系統(tǒng)的統(tǒng)計(jì)厌衙,用到的時(shí)候需要到處尋找。使用的時(shí)候記錄下來(lái)绞绒,方便自己也方便別人需要的時(shí)候查看婶希!
獲取當(dāng)前的時(shí)間戳10位
class func getCurrentTimeInTravael() -> TimeInterval {
return NSDate().timeIntervalSince1970
}
獲取當(dāng)前的日期
class func getCurrentDatePoint() -> String {
let now = Date()
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dformatter.string(from: now)
}
獲取明天年月日
class func getTomorrowDate() -> String {
let now = Date()
let dformatter = DateFormatter()
dformatter.dateFormat = "yyyy年MM月dd日"
let nextDate:TimeInterval = TimeInterval(24*60*60)
let sDate = now.addingTimeInterval(nextDate)
return dformatter.string(from: sDate)
}
時(shí)間戳轉(zhuǎn)換為Date類型
class func getDateFromTimeStamp(timeStamp:String) ->Date {
let interval:TimeInterval = TimeInterval.init(timeStamp)!
return Date(timeIntervalSince1970: interval)
}
時(shí)間格式轉(zhuǎn)換成時(shí)間戳
class func dateStrToTimeInterval(dateStr: String) -> Int {
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
let date = dateformatter.date(from: dateStr)
let dateTimeInterval:TimeInterval = date!.timeIntervalSince1970
return Int(dateTimeInterval)
}
時(shí)間格式轉(zhuǎn)換為Date類型
print(dateformatter.date(from: time)!)不要這樣打印去看date ,看到的跟你輸入的不一樣但是它并不是錯(cuò)誤的蓬衡∮麒荆可以采用下面的方式打印查看 print(dateformatter.string(from: date))
class func timeStrToDate(time:String) -> Date {
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy年MM月dd日"
return dateformatter.date(from: time)!
}
獲取當(dāng)前年份和月份和日
class func getCurrentYearMonthDay() -> (Int, Int, Int) {
let calendar = Calendar.current
let dateComponets = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from: Date())
return (dateComponets.year!, dateComponets.month!, dateComponets.day!)
}
獲取當(dāng)前月的總天數(shù)
class func getCurentMonthDays() -> Int {
let calendar = Calendar.current
let range = calendar.range(of: Calendar.Component.day, in: Calendar.Component.month, for: Date())
return range!.count
}
根據(jù)date獲取是周幾
func getWeedayFromeDate(date: Date) -> String {
let calendar = Calendar.current
let dateComponets = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.weekday,Calendar.Component.day], from: date)
//獲取到今天是周幾 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
let weekDay = dateComponets.weekday
switch weekDay {
case 1:
return "星期天"
case 2:
return "星期一"
case 3:
return "星期二"
case 4:
return "星期三"
case 5:
return "星期四"
case 6:
return "星期五"
case 7:
return "星期六"
default:
return ""
}
}
根據(jù)時(shí)間戳與當(dāng)前時(shí)間的比較(類似朋友圈的時(shí)間顯示)
class func compareCurrntTime(timeStamp:String) -> String {
//計(jì)算出時(shí)間戳距離現(xiàn)在時(shí)間的一個(gè)秒數(shù)(..s)
let interval:TimeInterval=TimeInterval(timeStamp)!
let date = Date (timeIntervalSince1970: interval)
var timeInterval = date.timeIntervalSinceNow
//得到的是一個(gè)負(fù)值 (加' - ' 得正以便后面計(jì)算)
timeInterval = -timeInterval
//根據(jù)時(shí)間差 做所對(duì)應(yīng)的文字描述 (作為返回文字描述)
var result:String
//一分鐘以內(nèi)
if interval < 60{
result = "剛剛"
return result
} else if Int(timeInterval/60) < 60{
//一小時(shí)以內(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:"%@小時(shí)前",String(Int((timeInterval/60)/60)))
return result
}else{
//超過(guò)一天的
let dateformatter = DateFormatter()
//自定義日期格式
dateformatter.dateFormat="yyyy年MM月dd日 HH:mm"
result = dateformatter.string(from: date as Date)
return result
}
}
得到本每周 開(kāi)始 和 結(jié)束的時(shí)間
class func getCurrentWeekDays() -> (String, String) {
let now = Date()
let calendar = Calendar.current
let dateComponets = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.weekday,Calendar.Component.day], from: now)
//獲取到今天是周幾 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
let weekDay = dateComponets.weekday
// 得到幾號(hào)
let day = dateComponets.day
// 計(jì)算當(dāng)前日期和這周的星期一和星期天差的天數(shù)
var firstDiff = 0
var lastDiff = 0
if weekDay == 1 {
firstDiff = 1
lastDiff = 0
} else {
firstDiff = calendar.firstWeekday - weekDay!+1;
lastDiff = 9 - weekDay!-1;
}
var firstDayComp = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from: now)
firstDayComp.day = day! + firstDiff
let firstDayOfWeek = calendar.date(from: firstDayComp)
var lastDayComp = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from: now)
lastDayComp.day = day! + lastDiff
let lastDayOfWeek = calendar.date(from: lastDayComp)
let formater = DateFormatter()
formater.dateFormat="yyyy年MM月dd日"
let startW = formater.string(from: firstDayOfWeek!)
let endW = formater.string(from: lastDayOfWeek!)
return (startW, endW)
}
檢測(cè)日期
class func checkDate(str: String) -> String {
let format = DateFormatter()
if let date = format.date(from: str) {
if Calendar.current.isDateInToday(date) {
return "日期是今天"
} else if Calendar.current.isDateInYesterday(date) {
return "日期是昨天"
} else if Calendar.current.isDateInTomorrow(date) {
return "日期是明天"
} else if Calendar.current.isDateInWeekend(date) {
return "日期是周末"
} else if Calendar.current.isDate(date, inSameDayAs: Date()) {
return "日期是今天,也就是傳入的和今天的是相同的日期"
} else {
return ""
}
}
return ""
}