swift 中日期擴展和元組的使用

嘗試寫swift 的擴展,發(fā)現(xiàn)元組是挺方便的

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
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,401評論 4 61
  • 軟件環(huán)境: 主機配置: 一共m1, m2, m3這三部機, 每部主機的用戶名都為centos 參考資料: 說明: ...
    咸魚翻身記閱讀 2,667評論 0 5
  • 啊要卡頭?。?! 簡歷時間厲害兔兔死投簡歷 嘉賓 吧!
    竹風林海閱讀 185評論 0 1
  • 1 100天了。 今天是我懷孕的第100天,我在想,我是否也愛了你100天呢? 懷孕過后的我每天都會看許多公眾號和...
    大風兒閱讀 625評論 2 2

友情鏈接更多精彩內(nèi)容