I am new to iOS development. I am trying to convert a date value which is in string format(2024-09-22) to 22 Sep 2024.
But I am getting:
Cannot call value of non-function type 'String?'
while doing date formatting.
I am getting this error in dateFormatter.dateFormat(from : dateString)
.
I have tried a below code for the formatting.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
dateFormatter.timeZone = TimeZone.current
dateFormatter.locale = Locale.current
let convertedDate = dateFormatter.dateFormat(from : dateString)
Could someone help me to solve this issue?
You first need to get Date
from given String
("2024-09-22" format: "yyyy-MM-dd"). And create one more DateFormatter to convert Date
to new String
:
let dateString = "2024-09-22"
let dateFormatterToDate = DateFormatter()
dateFormatterToDate.dateFormat = "yyyy-MM-dd"
let date = dateFormatterToDate.date(from: dateString)
let dateFormatterFromDate = DateFormatter()
dateFormatterFromDate.dateFormat = "dd MMM yyyy"
let result = dateFormatterFromDate.string(from: date!) // 22 Sep 2024