首页 > 学院 > 开发设计 > 正文

swift 对日期的处理大全( 类扩展 Date+Extension 和 公共类 DateClass)

2019-11-06 09:37:32
字体:
来源:转载
供稿:网友

类扩展 Date+Extension

import UIKitextension Date {    //MARK: - 获取日期各种值    //MARK: 年    func year() ->Int {        let calendar = NSCalendar.current        let com = calendar.dateComponents([.year,.month,.day], from: self)        return com.year!    }    //MARK: 月    func month() ->Int {        let calendar = NSCalendar.current        let com = calendar.dateComponents([.year,.month,.day], from: self)        return com.month!            }    //MARK: 日    func day() ->Int {        let calendar = NSCalendar.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/86400) // 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 {        //1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.        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: - 日期的一些比较    //是否是今天    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    }    //是否是这个月    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    }}

DateClass

import UIKitclass 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/86400) // 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 {        //1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)        let firstWeekDay = (calendar as NSCalendar?)?.ordinality(of: NSCalendar.Unit.weekday, in: NSCalendar.Unit.weekOfMonth, for: Date())        return firstWeekDay! - 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 getfirstWeekDayInMonth(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 getMonthDate(year:Int,month:Int) ->Date {        let dateFormatter = DateFormatter()        dateFormatter.dateFormat = "yyyy-MM"        let date            = dateFormatter.date(from: String(year)+"-"+String(month))        let calendar = Calendar(identifier:Calendar.Identifier.gregorian)        var com = calendar.dateComponents([.year,.month,.day], from: date!)        com.month = month        com.day = 1        return calendar.date(from: com)!    }    //MARK: 比较时间先后    static func compareOneDay(oneDay:Date, withAnotherDay anotherDay:Date) -> Int {        let dateFormatter: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  is in the future        if(result == ComparisonResult.orderedDescending ) {            return 1        }        //Date1 is in the past        else if(result == ComparisonResult.orderedAscending) {            return 2        }        //Both dates are the same        else {            return 0        }    }    //MARK: 时间与时间戳之间的转化    //将时间转换为时间戳    static func stringToTimeStamp(_ stringTime:String)->String {        let dfmatter = DateFormatter()        dfmatter.dateFormat = "yyyy-MM-dd HH:mm:ss"        dfmatter.locale = Locale.current        let date = dfmatter.date(from: stringTime)        let dateStamp:TimeInterval = date!.timeIntervalSince1970        let dateSt:Int = Int(dateStamp)        return String(dateSt)    }    //将时间戳转换为年月日    static func timeStampToString(_ timeStamp:String)->String {        let string = NSString(string: timeStamp)        let timeSta:TimeInterval = string.doubleValue        let dfmatter = DateFormatter()        dfmatter.dateFormat="yyyy年MM月dd日"        let date = Date(timeIntervalSince1970: timeSta)        return dfmatter.string(from: date)    }    //将时间戳转换为具体时间    static func timeStampToStringDetail(_ timeStamp:String)->String {        let string = NSString(string: timeStamp)        let timeSta:TimeInterval = string.doubleValue        let dfmatter = DateFormatter()        dfmatter.dateFormat="yyyy年MM月dd日HH:mm:ss"        let date = Date(timeIntervalSince1970: timeSta)        return dfmatter.string(from: date)    }    //将时间戳转换为时分秒    static func timeStampToHHMMSS(_ timeStamp:String)->String {        let string = NSString(string: timeStamp)        let timeSta:TimeInterval = string.doubleValue        let dfmatter = DateFormatter()        dfmatter.dateFormat="HH:mm:ss"        let date = Date(timeIntervalSince1970: timeSta)        return dfmatter.string(from: date)    }    //获取系统的当前时间戳    static func getStamp()->Int{        //获取当前时间戳        let date = Date()        let timeInterval:Int = Int(date.timeIntervalSince1970)        return timeInterval    }    //月份数字转汉字    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)        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表