Difference between 2 dates in weeks and days using swift 3 and xcode 8

前端 未结 4 2116
情话喂你
情话喂你 2020-12-05 02:13

I am trying to calculate the difference between 2 dates (one is the current date and the other from datepicker) in weeks and days then displaying the result on a label, that

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 02:51

    Swift 4,5

    You can find date different in weeks with this one line code.

    let totalWeeks = Calendar.current.dateComponents([.weekOfMonth], from: Date(), to: endDate).weekOfMonth!
    
    //for totalDays
    let totalDays = Calendar.current.dateComponents([.day], from: Date(), to: endDate).day!
    

提交回复
热议问题