I'm pretty new to programming and I'm working with a database that has a date field. All the data in the field are saved as strings on the yyyymmdd format. I want to convert those to something friendlier to display to the user. I created the following struct and function to do it. It works, but I was wondering if there are any simpler ideas.
struct MyDate {
var day: String
var month: String
var year: String
}
func ConvertToUsefullDate (_ date: String) -> MyDate {
var myDate: MyDate = .init(day: "", month: "", year: "")
myDate.year =
String(date[date.index(date.startIndex, offsetBy: 0)]) +
String(date[date.index(date.startIndex, offsetBy: 1)]) +
String(date[date.index(date.startIndex, offsetBy: 2)]) +
String(date[date.index(date.startIndex, offsetBy: 3)])
myDate.month =
String(date[date.index(date.startIndex, offsetBy: 4)]) +
String(date[date.index(date.startIndex, offsetBy: 5)])
myDate.day =
String(date[date.index(date.startIndex, offsetBy: 6)]) +
String(date[date.index(date.startIndex, offsetBy: 7)])
switch myDate.month {
case "01": myDate.month = "Jan"
case "02": myDate.month = "Feb"
case "03": myDate.month = "Mar"
case "04": myDate.month = "Apr"
case "05": myDate.month = "May"
case "06": myDate.month = "Jun"
case "07": myDate.month = "Jul"
case "08": myDate.month = "Aug"
case "09": myDate.month = "Sep"
case "10": myDate.month = "Oct"
case "11": myDate.month = "Nov"
case "12": myDate.month = "Dec"
default: myDate.month = "Invalid"
}
return myDate
}
Thank you -- all feedback is welcome.
Daniel