Swift days between two NSDates

后端 未结 27 1585
清歌不尽
清歌不尽 2020-11-27 13:30

I\'m wondering if there is some new and awesome possibility to get the amount of days between two NSDates in Swift / the \"new\" Cocoa?

E.g. like in Ruby I would do:

27条回答
  •  盖世英雄少女心
    2020-11-27 13:45

      func completeOffset(from date:Date) -> String? {
    
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .brief
    
        return  formatter.string(from: Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date, to: self))
    
    
    
    
    }
    

    if you need year month days and hours as string use this

    var tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())!

    let dc = tomorrow.completeOffset(from: Date())

提交回复
热议问题