Subtract 7 days from current date

前端 未结 11 1083
灰色年华
灰色年华 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 4.2 iOS 11.x Babec's solution, pure Swift

    extension Date {
      static func changeDaysBy(days : Int) -> Date {
        let currentDate = Date()
        var dateComponents = DateComponents()
        dateComponents.day = days
        return Calendar.current.date(byAdding: dateComponents, to: currentDate)!
      }
    }
    

提交回复
热议问题