Search code examples
iosswiftnsdatensdateformatter

remove time from a date like this 2016-02-10 00:00:00


hello I have a date like this

2016-02-10 00:00:00

I want to get only date from it in this style

14.05.2016 or 14-05-2016

This is what I have tried

let dateFormatter = NSDateFormatter()
let date = "2016-02-10 00:00:00"
dateFormatter.dateFormat = "dd-MM-yyyy"
let newdate = dateFormatter.dateFromString(date)
print(newdate) //nil is coming

Solution

  • okay I solved this myself

      let date = "2016-02-10 00:00:00"
      let dateFormatter = NSDateFormatter()
    
      dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
      let dateFromString : NSDate = dateFormatter.dateFromString(date)!
      dateFormatter.dateFormat = "dd-MM-yyyy"
      let datenew= dateFormatter.stringFromDate(dateFromString)
    
        print(datenew)