skip to Main Content

In my iOS app, I am getting the date format as 20231220T090000 for my calendar event in response. How will I convert this datetime from the date?

How to convert it to 20 Dec 2023, 9 AM?

I can’t figure out the correct date format for it.

2

Answers


  1. In your example, 20231220T090000 represents a date in the following format:

    yyyyMMdd'T'HHmmss
    

    And you’re trying to turn it into the following format:

    dd MMM yyyy, h a
    

    You can use DateFormatter to parse the date from your input string and then re-format it into another date with the target format.

    Something like this could achieve what you’re looking for:

    let inFormat = DateFormatter()
    inFormat.dateFormat = "yyyyMMdd'T'HHmmss"
    
    let inDate = inFormat.date(from: "20231220T090000")
    
    let outFormat = DateFormatter()
    outFormat.dateFormat = "dd MMM yyyy, h a"
    
    let outString = outFormat.string(from: inDate!)
    
    print(outString) // 20 Dec 2023, 9 AM
    

    This can help you if you need the date format to be fixed. If you wish to display this date to a user, you should instead set .dateStyle and .timeStyle to the outFormat.

    Rob’s answer is more detailed and informative.

    Login or Signup to reply.
  2. You need a date formatter to parse the input:

    let parsingFormatter = DateFormatter()
    parsingFormatter.locale = Locale(identifier: "en_US_POSIX")
    parsingFormatter.dateFormat = "yyyyMMdd'T'HHmmss"
    

    By the way, you do not specify in what timezone the input string represents. Often it is in GMT/UTC/Zulu, in which case you would also include:

    parsingFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    

    Note, this input string is likely in a fixed-format and, if so, it is important to set the locale of this date parsing formatter, as shown above, in case the user device is not using a Gregorian calendar.

    Anyway, you also need another date formatter to produced your output string:

    let outputFormatter = DateFormatter()
    outputFormatter.dateFormat = "d MMM yyyy',' h a" 
    

    No locale or timeZone is needed in this second formatter, as you undoubtedly want this output string formatted using the device default settings.

    As an aside, we would generally avoid using dateFormat for output strings, but rather use dateStyle and timeStyle instead. (See the distinction between “user visible” and “fixed format” date strings in the DateFormatter documentation.) Using date/time styles ensures that the date string is produced in a standard format, and one that is localized in accordance with the device settings, e.g.:

    let outputFormatter = DateFormatter()
    outputFormatter.dateStyle = .medium
    outputFormatter.timeStyle = .short
    

    Or, if you insist on this custom format (e.g., showing hours without minutes), you might consider using a localized date format string:

    let outputFormatter = DateFormatter()
    outputFormatter.setLocalizedDateFormatFromTemplate("dMMMyyyyha")
    

    Anyway, now that you have your two formatters, you can parse the input and prepare the output string like so:

    let input = "20231220T090000"
    if let date = parsingFormatter.date(from: input) {
        let result = outputFormatter.string(from: date)
        …
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search