ios - How to convert the each date components to particular date format in swift 2 -


i can obtain individual date components as:

let datestring = string(datamodel.datecompletedday) + string(datamodel.datecompletedmonth) + string(datamodel.datecompletedyear) print(datestring) //2532016 

now how can convert dd/mm/yyyy format?

i suggest use calendar, datecomponents , dateformatter (swift 3)

let components = datecomponents(year:datamodel.datecompletedyear, month: datamodel.datecompletedmonth, day: datamodel.datecompletedday) let calendar = calendar(identifier: .gregorian)! let date = calendar.date(from: components)! let formatter = dateformatter() formatter.dateformat = "dd/mm/yyyy" let datestring = formatter.string(from: date) 

additionally simple solution supposed work in swift 2

let datestring = string(format: "%02ld/%02ld/%ld", datamodel.datecompletedday, datamodel.datecompletedmonth, datamodel.datecompletedyear) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -