//
// DateHelper.swift
// 自定义tabBar
//
// Created by HR on 2018/5/27.
// Copyright © 2018年 HR. All rights reserved.
//
import Foundation
import UIKit
public extension Date {
// - Returns: 年份
func year() -> Int {
let calendar = Calendar.current
let com = calendar.dateComponents([.year,.month,.day], from: self)
return com.year!
}
// 月份
func month() -> Int {
let calendar = Calendar.current
let com = calendar.dateComponents([.year,.month,.day], from: self)
return com.month!
}
// MARK:日期
func day() -> Int {
let calendar = Calendar.current
let com = calendar.dateComponents([.year,.month,.day], from: self)
return com.day!
}
// MARK: 星期几
func weekDay() -> Int {
let interval = Int(self.timeIntervalSince1970)
let days = Int(interval/24/60/60)
let weekday = ((days + 4) % 7 + 7) % 7
return weekday == 0 ? 7 : weekday
}
// MARK: 当月天数
func countOfDaysInMonth() -> Int {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: self)
return (range?.length)!
}
// MARK: 当月第一天是星期几
func firstWeekDay() -> Int {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let firstWeekDay = (calendar as NSCalendar?)?.ordinality(of: NSCalendar.Unit.weekday, in: NSCalendar.Unit.weekOfMonth, for: self)
return firstWeekDay! - 1
}
// MARK: 日期的一些比较
// MARK: 是否是今天
func isToday() -> Bool {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: self)
let comNow = calendar.dateComponents([.year,.month,.day], from: Date())
return com.year == comNow.year && com.month == comNow.month && com.day == comNow.day
}
// MARK: 是否是这个月
func isThisMonth() -> Bool {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: self)
let comNow = calendar.dateComponents([.year,.month,.day], from: Date())
return com.year == comNow.year && com.month == comNow.month
}
}
class DateClass {
// MARK: 当前时间相关
// MARK: 今年
static func CurrentYear() -> Int {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
return com.year!
}
// MARK: 今月
static func currentMonth() -> Int {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
return com.month!
}
// MARK: 今日
static func currentDay() -> Int {
let calendar = NSCalendar.current
let com = calendar.dateComponents([.year,.month,.day], from: Date())
return com.day!
}
// MARK: 今天是星期几
static func currentWeekDay() -> Int {
let interval = Int(Date().timeIntervalSince1970)
let days = Int(interval/24/60/60)
let weekday = ((days + 4)%7 + 7)%7
return weekday == 0 ? 7 : weekday
}
// MARK: 本月天数
static func countOfDaysInCurrentMonth() -> Int {
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: Date())
return (range?.length)!
}
// MARK: 当月第一天是星期几
static func firstWeekDayInCurrentMonth() -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let date = dateFormatter.date(from: String(Date().year()) + "-" + String(Date().month()))
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let comps = (calendar as NSCalendar?)?.components(NSCalendar.Unit.weekday, from: date!)
var week = comps?.weekday
if week == 1 {
week = 8
}
return week! - 1
}
// MARK: 获取指定日期的各种值
//根据年月得到某月天数
static func getCountOfDaysInMonth(year: Int, month: Int) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let date = dateFormatter.date(from: String(year) + "-" + String(month))
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for:date!)
return (range?.length)!
}
// MARK: 根据年月获得某月第一天是星期几
static func getWeekDayInMonth(year: Int, month: Int) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let date = dateFormatter.date(from: String(year) + "-" + String(month))
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let comps = (calendar as NSCalendar?)?.components(NSCalendar.Unit.weekday, from: date!)
let week = comps?.weekday
return week! - 1
}
// MARK: date转日期字符串
static func dateToDateString(_ date: Date, dateFormat: String) -> String {
let timeZone = NSTimeZone.local //时区
let formatter = DateFormatter()
formatter.timeZone = timeZone
formatter.dateFormat = dateFormat
let date = formatter.string(from: date)
return date
}
// MARK: 日期字符串转date
static func dateStringToDate(_ dataStr: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dataStr)
return date!
}
// MARK: 时间字符串转date
static func timeStringToDate(_ dateStr: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.date(from: dateStr)
return date!
}
// MARK: 计算天数差
static func dateDifferrence(_ dateA:Date, from dateB: Date) -> Double {
let interval = dateA.timeIntervalSince(dateB)
return interval/86400
}
// MARK: 比较时间先后
static func compareOneDay(oneDay: Date, withAnotherDay anotherDay:Date) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let oneDayStr: String = dateFormatter.string(from: oneDay)
let anotherDayStr: String = dateFormatter.string(from: anotherDay)
let dateA = dateFormatter.date(from: oneDayStr)
let dateB = dateFormatter.date(from: anotherDayStr)
let result: ComparisonResult = (dateA?.compare(dateB!))!
//Date1 早
if result == ComparisonResult.orderedDescending {
return 1
//Date1 晚
}else if result == ComparisonResult.orderedAscending {
return 2
//相等
}else {
return 0
}
}
// MARK: 将时间转化为时间戳
static func stringTotimeStamp(_ stringTime: String) -> Int {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale.current
let date = dateFormatter.date(from: stringTime)
let dateStamp: TimeInterval = date!.timeIntervalSince1970
let dateSt: Int = Int(dateStamp)
return dateSt
}
// MARK: 将时间戳转换为年月日
static func timeStampToString(_ timeStamp: String) -> String {
let string = NSString(string: timeStamp)
let timeSta: TimeInterval = string.doubleValue
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日"
let date = Date(timeIntervalSince1970: timeSta)
return dateFormatter.string(from: date)
}
// MARK: 将时间戳装换为具体时间
static func timeStampToStringDetail(_ timeStamp: String) -> String {
let string = NSString(string: timeStamp)
let timeSta: TimeInterval = string.doubleValue
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy年MM月dd日HH:mm:ss"
let date = Date(timeIntervalSince1970: timeSta)
return dateFormatter.string(from: date)
}
// MARK: 将时间戳转化为时分秒
static func timeStampToHHMMSS(_ timeStamp: String) -> String {
let string = NSString(string: timeStamp)
let timeSta:TimeInterval = string.doubleValue
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let date = Date(timeIntervalSince1970: timeSta)
return dateFormatter.string(from: date)
}
// MARK: 获取系统当前时间戳
static func getCurrentStamp() -> Int {
let date = Date()
let timeInterval: Int = Int(date.timeIntervalSince1970)
return timeInterval
}
// MARK: 月份数字转汉字
static func numberToChina(monthNum: Int) -> String {
let chinaArray = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
let chinaStr: String = chinaArray[monthNum - 1]
return chinaStr
}
// MARK: 数字前面补0
static func add0BeforeNumber(_ number: Int) -> String {
if number >= 10 {
return String(number)
}else{
return "0" + String(number)
}
}
// MARK: 将时间显示为(几分钟前,几小时前,几天前)
static func compareCurrentTime(str: String) -> String {
let tiemDate = self.timeStringToDate(str)
let currentDate = NSDate()
let tiemInterval = currentDate.timeIntervalSince(tiemDate)
var temp: Double = 0
var result: String = ""
if tiemInterval/60 < 1 {
result = "刚刚"
}else if tiemInterval/60 < 60 {
temp = tiemInterval/60
result = "\(Int(temp))分钟前"
}else if tiemInterval/60/60 < 24 {
temp = tiemInterval/60/60
result = "\(Int(temp))小时前"
}else if tiemInterval/60/60/24 < 30 {
temp = tiemInterval/60/60/24
result = "\(Int(temp))天前"
}else if tiemInterval/(60 * 60 * 24 * 30) < 12 {
temp = tiemInterval/(60 * 60 * 24 * 30)
result = "\(Int(temp))个月前"
}else {
temp = tiemInterval/(12 * 30 * 24 * 60 * 60)
result = "\(Int(temp))年前"
}
return result
}
}
网友评论