Truncate NSDate (Zero-out time)

前端 未结 6 898
孤街浪徒
孤街浪徒 2020-12-02 19:02

I want to generate a new NSDate with 0 hours, 0 minutes, and 0 seconds for time. The source date can be any r

6条回答
  •  情书的邮戳
    2020-12-02 19:18

    Swift 3

    extension Date {
        func trimTime() -> Date {
            var boundary = Date()
            var interval: TimeInterval = 0
            _ = Calendar.current.dateInterval(of: .day, start: &boundary, interval: &interval, for: self)
    
            return Date(timeInterval: TimeInterval(NSTimeZone.system.secondsFromGMT()), since: boundary)
        }
    }
    

提交回复
热议问题