1

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

2 Answers2

2

try this:

let unformattedDate = your unformatted date

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "yyyy-MM-dd" (or whatever your current format is)
let newDate : NSDate = dateFormatter.date(from: unformattedDate!)! as NSDate

//new date format, change to the format you like (I like MM/dd/yyyy)

dateFormatter.dateFormat = "MM/dd/yyyy"
let formattedDate = dateFormatter.string(from: dateFromStringstartDate as Date)

then you can use formattedDate as you'd like

use MMMM to represent month

use dddd to represent day of week

use dd to represent day

use yyyy to represent year

and use HH:mm:ss to represent time

for more formats check out the image below! image of all formats

1

There's an existing type Date that you should probably use instead of your own type.

To create a Date object from a formatted string, use the DateFormatter:

let inputDateString = "20200428"

let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyyMMdd"
inputFormatter.locale = Locale(identifier: "en_US_POSIX")

// date is optional
let date = inputFormatter.date(from: inputDateString) 

Once you have a Date object, you could output it in whatever format you'd like:

let outputFormatter = DateFormatter()

// 28 April, 2020
outputFormatter.dateFormat = "dd MMMM, yyyy" 

Or, better, use the locale-aware way:

let outputFormatter = DateFormatter()
outputFormatter.dateStyle = .long
outputFormatter.timeStyle = .none

Then you can convert it to a String:

let outputDateString = outputFormatter.string(from: date!) // ! for simplicity

print(outputDateString)
New Dev
  • 48,427
  • 12
  • 87
  • 129
  • Thanks guys! Those are great. I didn’t know we could use DateFormatter() the other way around. To go from type Date to String I knew, not the other way. Kudos – Daniel Poit F May 29 '20 at 01:47