-3

I have to change the format of the date from dd/MM to dd/MM/yyyy and I use this code

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM"
let date = dateFormatter.date(from: "28/07")
dateFormatter.dateFormat = "dd/MM/yyyy"
let stringDate = dateFormatter.string(from: date ?? Date())

The output is: 28/07/2000. What is wrong?

1 Answers1

0

If you want to add current year, use this:

let date = Date()
let calender = Calendar.current
let components = calender.dateComponents([.year], from: date)

let year = components.year

and add this 'year' after your date string. Then you will have a date and current year.

koen
  • 5,383
  • 7
  • 50
  • 89
Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20