美文网首页
swift 中日期扩展和元组的使用

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

作者: 陈藩 | 来源:发表于2018-05-06 23:30 被阅读0次

尝试写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 中日期扩展和元组的使用

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

  • Swift超基础语法(元组篇)

    你对Swift中的元组了解多少呢?...很有自信嘛...看完这篇文章再说喽 元组 元组是Swift中特有的,OC中...

  • 2019-05-05: 七:Swift中元祖的使用?

    一:Swift中元祖的使用? 二:元祖的介绍 三:元组的定义 四:元组的简单使用

  • Swift3知识点梳理(二):元组

    1. 为什么使用元组 元组类型是swift中很有特色的类型,也是我很喜欢的一个类型。之前无论在使用c++,c...

  • Swift语法--函数

    Swift中的函数几个特点: func 开头,返回值在函数定义末尾 可以返回元组,并给元组命名 可以使用默认参数 ...

  • Swift--扩展

    扩展的概念 扩展计算属性 扩展方法 扩展构造函数 扩展下标 扩展的概念 在Swift中可以使用一种扩展机制,在原有...

  • 对swift中的扩展进行"分组"

    在swift中对类和struct进行扩展或使用协议扩展(本质是mixin),是非常简单且实用的语法。但是语法糖太好...

  • Swift 绘制圆形头像

    使用 Swift 绘制圆形头像,代码如下: Swift2.3 版本的写法(写在 UIImage 的扩展中) Swi...

  • Swift 基本运算符

    Swift中的基本运算符及其使用方法和C、Java中的大致相同,不再赘述。值得注意的是前篇文章提到的元组是可以使用...

  • Python基础_05:元组(2019-1-14)

    元组 python中元组和列表类似不同之处在于元组中的元素不可修改元组使用(),列表使用[]return a,b,...

网友评论

      本文标题:swift 中日期扩展和元组的使用

      本文链接:https://www.haomeiwen.com/subject/mpmprftx.html