swift 中日期扩展和元组的使用

尝试写swift 的扩展,发现元组是挺方便的

import Foundation

extension Date {
    
    /*
     * 获取这个月有多少天
     */
    func getMonthHowManyDays() -> Int {
        
        return (NSCalendar.current.range(of: .day, in: .month, for: self as Date)?.count)!
    }
    
    /*
     * 获取日期是星期几
     */
    func getDateWeekDay() -> NSInteger {
        let dateFmt = DateFormatter.init()
        dateFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let interval = Int(self.timeIntervalSince1970)
        let days = Int(interval/86400)
        let weekday = ((days + 4)%7+7)%7
        
        return weekday;
    }
    
    /*
     * 获取这个月第一天是星期几
     */
    func getMonthFirstWeekDay() ->Int {
        //1.sun 2.Mon 3.Thes ... 7 sat
        let calendar = Calendar.current
        var com = calendar.dateComponents([.year,.month,.day], from: self as Date)
        
        //设置成第一天
        com.day = 1;
        let date = calendar.date(from: com)
        let firstWeekDay = calendar.ordinality(of: .weekday, in: .weekOfMonth, for: date!)
        
        return firstWeekDay! - 1
    }
   
    /*
     * 获取当前Year
     */
    func getYear() ->Int {
        let callendar = Calendar.current
        let com = callendar.dateComponents([.year,.month,.day], from: self as Date)
        
        return com.year!
    }
    
    /*
     * 获取当前Month
     */
    func getMonth() -> Int {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.year,.month,.day], from: self as Date)
        
        return com.month!
    }
    
    /*
     * 获取当前Day
     */
    func getDay() -> Int {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.year,.month,.day], from:self as Date)
        
        return com.day!
    }
    
    /*
     * 获取当前 hour
     */
    func getHour() -> Int {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.hour,.minute,.second], from:self as Date)
        
        return com.hour!
    }
    
    /*
     * 获取当前 min
     */
    func getMin() -> Int {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.hour,.minute,.second], from:self as Date)
        
        return com.minute!
    }
    
    /*
     * 获取当前 second
     */
    func getSecond() -> Int {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.hour,.minute,.second], from:self as Date)
        return com.second!
    }
    
    /*
     * 实用元组 获取当前 年,月,日
     *
     */
    func getYearMonthDay() ->(year:Int,month:Int,day:Int) {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.year,.month,.day], from:self as Date)
        
        return(com.year!,com.month!,com.day!)
    }
    
    /*
     * 实用元组 获取当前 时,分,秒
     *
     */
    func getHourMinSec() ->(hour:Int,min:Int,sec:Int) {
        let  callendar = Calendar.current
        let com  = callendar.dateComponents([.hour,.minute,.second], from:self as Date)
        
        return(com.hour!,com.minute!,com.second!)
    }
    
    /*
     * 获取当前的 年-月-日
     */
    func getCurrentDateStr() -> String {
        let dateFmt = DateFormatter.init()
        dateFmt.dateFormat = "yyyy-MM-dd"
        
        return dateFmt.string(from: self as Date)
    }
    
    /*
     * 获取当前的 年-月-日 时:分:秒
     */
    func getCurrentTimeStr() -> String {
        let dateFmt = DateFormatter.init()
        dateFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        return dateFmt.string(from: self as Date)
    }

}


使用

            let date = Date.init()
//            let year = date.getYear();
//            let month = date.getMonth();
//            let day = date.getDay();
//
//            let hour = date.getHour()
//            let min = date.getMin()
//            let sec = date.getSecond()
            
            let yearMonthDay = date.getYearMonthDay()
            let year = yearMonthDay.year;
            let month = yearMonthDay.month;
            let day = yearMonthDay.day
            
            let hourMinSec = date.getHourMinSec()
            let hour = hourMinSec.hour
            let min = hourMinSec.min
            let sec = hourMinSec.sec

你可能感兴趣的:(swift 中日期扩展和元组的使用)