swift-获取日期信息

2020-06-16  本文已影响0人  只会ctrl_c_v
/**
     获取日期信息
     - parameter i: 传参为今天开始的第几天 今天为0, 明天为1, -1则代表昨天 以此类推
     - return    时间信息
 */
   func getDateInfo(i: Int) -> (yearStr: String, monthStr: String, dayStr: String) {
        
        //获取当前时间
        let cuurentDate = Date.init()
        //对自己需要的时间进行处理
        let needTime: TimeInterval = TimeInterval(i * (24*60*60))
        let needDate = cuurentDate.addingTimeInterval(needTime)
    
        //格式化
        let yearFormatter = DateFormatter()
        yearFormatter.dateFormat = "yyyy"
        let monthFormatter = DateFormatter()
        monthFormatter.dateFormat = "MM"
        let dayFormatter = DateFormatter()
        dayFormatter.dateFormat = "dd"
        
        let year = yearFormatter.string(from: needDate)
        let month = monthFormatter.string(from: needDate)
        let day = dayFormatter.string(from: needDate)
        return (year, month, day)
    }

上一篇下一篇

猜你喜欢

热点阅读