美文网首页程序员
关于Date的一些常用的方法总结

关于Date的一些常用的方法总结

作者: BeSt2wazi | 来源:发表于2017-03-10 14:44 被阅读0次

    最近在新的项目中用了很多关于时间计算的东西,各种需求也是让人难受的不行,其中也遇到不少关于时区的坑,特此前来总结一下关于date的常用延展方法,以后还会陆续补充

    把当前时间转换成当前时区

    func passDate() -> Date{
        let zone = NSTimeZone.system
        let time:Int = zone.secondsFromGMT(for: self)
        let nowDate = self.addingTimeInterval(TimeInterval(time))
        return nowDate
    }
    

    date转字符串

    /// - Parameters:
    ///   - date: 某天
    ///   - string: 时间格式
    /// - Returns: string
    func getSomeDayToString(date:Date,string:String) -> String {
        let nowDate = date
        let timeZone = TimeZone.init(identifier: "UTC")
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = string
        return formatter.string(from: nowDate)
    }
    

    获取某天的指定时间

    /// - Parameter time: 时间 (19:00)
    /// - Returns: date
    func getCurrentDaySomeTime(time:String,someday:Date) -> Date {
        let timeZone = TimeZone.init(identifier: "UTC")
        let formatter = DateFormatter()
        formatter.timeZone = timeZone
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = "yyyy年MM月dd日"
        var today = formatter.string(from: someday)
        today = today + " " + time
        let newformatter = DateFormatter()
        newformatter.dateFormat = "yyyy年MM月dd日 HH:mm"
        newformatter.timeZone = timeZone
        newformatter.locale = Locale.init(identifier: "zh_CN")
        let newDate = newformatter.date(from: today)!
        return newDate
    }
    

    date转时间戳

    func dateToTimestamp(date:Date) -> TimeInterval {
        let invale = date.timeIntervalSince1970
        return invale
    }
    

    获取周六晚上七点的时间

    func getSatdayNight()->Date{
        
        let gmt = NSTimeZone(abbreviation: "GMT")
        let gregorian = NSCalendar(identifier: .gregorian)
        gregorian?.timeZone = gmt as! TimeZone
        var components = gregorian?.components([ .weekday,.day,.hour,.minute,.second], from: self)
        components?.setValue(6 - (components?.weekday)!, for: .weekday)
        components?.setValue(1, for: .day)
        components?.setValue(19 - (components?.hour)! - 1, for: .hour)
        components?.setValue(60 - (components?.minute)! - 1, for: .minute)
        components?.setValue(60 - (components?.second)!, for: .second)
        return (gregorian?.date(byAdding: components!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
        
    }
    

    昨天

    func getLastDay() ->Date {
    
        let calendar  = NSCalendar.init(identifier: .gregorian)
        var comps = calendar?.components(.day, from: self)
        comps?.setValue(-1, for: .day)
        
        return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
        
    }
    

    前三天

    func getLastThreeDay() ->Date {
        
        let calendar  = NSCalendar.init(identifier: .gregorian)
        var comps = calendar?.components(.day, from: self)
        comps?.setValue(-2, for: .day)
        
        return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
        
    }
    

    今天稍后两小时

    func getTodaylater() -> Date{
        let calendar  = NSCalendar.init(identifier: .gregorian)
        var comps = calendar?.components(.hour, from: self)
        comps?.setValue(2, for: .hour)
    
        return (calendar?.date(byAdding: comps!, to: self, options: NSCalendar.Options(rawValue: 0))!)!
    }
    

    比较当前时间和指定时间

    /// - Parameter customDate: 要比较的时间
    /// - Returns: 早于当前时间返回YES,否则NO
    func compareTime(customDate:Date) -> Bool {
        let isTrue:ComparisonResult = self.passDate().compare(customDate)
        if isTrue == ComparisonResult.orderedAscending {
            return true
        }else{
            return false
        }
    }

    相关文章

      网友评论

        本文标题:关于Date的一些常用的方法总结

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