iphone Get current year as string

后端 未结 5 1245
迷失自我
迷失自我 2021-02-01 01:33

How do I get current year as string in Obj-C ?

Also how do I compare the same using another year value ?

Is it advisable to do a string comparision OR dirctly ye

5条回答
  •  我在风中等你
    2021-02-01 02:30

    Swift

    Easier way to get any elements of date as an optional String.

    extension Date {
    
      // Year 
      var currentYear: String? {
        return getDateComponent(dateFormat: "yy")
        //return getDateComponent(dateFormat: "yyyy")
      }
    
    
      func getDateComponent(dateFormat: String) -> String? {
        let format = DateFormatter()
        format.dateFormat = dateFormat
        return format.string(from: self)
      }
    
    
    }
    
    
    print("-- \(Date().currentYear)")  // result -- Optional("2017")
    

提交回复
热议问题