Subtract 7 days from current date

前端 未结 11 920
灰色年华
灰色年华 2021-01-29 22:41

It seems that I can\'t subtract 7 days from the current date. This is how i am doing it:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:N         


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 23:33

    Swift operator extension:

    extension Date {
        
        static func -(lhs: Date, rhs: Int) -> Date {
            return Calendar.current.date(byAdding: .day, value: -rhs, to: lhs)!
        }
    }
    

    Usage

    let today = Date()
    let yesterday = today - 7
    

提交回复
热议问题