How do you compare just the time of a Date in Swift?

后端 未结 8 950
挽巷
挽巷 2020-12-29 10:05

I have two Date Objects:

  1. 2017-01-13 11:40:17 +0000

  2. 2016-03-15 10:22:14 +0000

I need to compare

8条回答
  •  粉色の甜心
    2020-12-29 11:00

    This is the route I took in the end, which makes it easy to compare just the time of a Date in swift

    New Object Time:

    class Time: Comparable, Equatable {
    init(_ date: Date) {
        //get the current calender
        let calendar = Calendar.current
    
        //get just the minute and the hour of the day passed to it
        let dateComponents = calendar.dateComponents([.hour, .minute], from: date)
    
            //calculate the seconds since the beggining of the day for comparisions
            let dateSeconds = dateComponents.hour! * 3600 + dateComponents.minute! * 60
    
            //set the varibles
            secondsSinceBeginningOfDay = dateSeconds
            hour = dateComponents.hour!
            minute = dateComponents.minute!
        }
    
        init(_ hour: Int, _ minute: Int) {
            //calculate the seconds since the beggining of the day for comparisions
            let dateSeconds = hour * 3600 + minute * 60
    
            //set the varibles
            secondsSinceBeginningOfDay = dateSeconds
            self.hour = hour
            self.minute = minute
        }
    
        var hour : Int
        var minute: Int
    
        var date: Date {
            //get the current calender
            let calendar = Calendar.current
    
            //create a new date components.
            var dateComponents = DateComponents()
    
            dateComponents.hour = hour
            dateComponents.minute = minute
    
            return calendar.date(byAdding: dateComponents, to: Date())!
        }
    
        /// the number or seconds since the beggining of the day, this is used for comparisions
        private let secondsSinceBeginningOfDay: Int
    
        //comparisions so you can compare times
        static func == (lhs: Time, rhs: Time) -> Bool {
            return lhs.secondsSinceBeginningOfDay == rhs.secondsSinceBeginningOfDay
        }
    
        static func < (lhs: Time, rhs: Time) -> Bool {
            return lhs.secondsSinceBeginningOfDay < rhs.secondsSinceBeginningOfDay
        }
    
        static func <= (lhs: Time, rhs: Time) -> Bool {
            return lhs.secondsSinceBeginningOfDay <= rhs.secondsSinceBeginningOfDay
        }
    
    
        static func >= (lhs: Time, rhs: Time) -> Bool {
            return lhs.secondsSinceBeginningOfDay >= rhs.secondsSinceBeginningOfDay
        }
    
    
        static func > (lhs: Time, rhs: Time) -> Bool {
            return lhs.secondsSinceBeginningOfDay > rhs.secondsSinceBeginningOfDay
        }
    }
    

    Date Extension for easy access: //Adds ability to just get the time from a date:

    extension Date {
        var time: Time {
            return Time(self)
        }
    }
    

    Example:

    let firstDate = Date()
    let secondDate = firstDate
    
    //Will return true
    let timeEqual = firstDate.time == secondDate.time
    

提交回复
热议问题