Avatar ·

Converting String to Date in Swift: Complete Guide

Hi everyone! I'm new to Swift and can't figure out converting a string to a Date object. My task is to get a date from a string in the format "2023-10-15 14:30:00", but I keep getting nil.\n\nHere's my code:\n\n```swift\nlet dateString = "2023-10-15 14:30:00"\nlet dateFormatter = DateFormatter()\ndateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"\nlet date = dateFormatter.date(from: dateString)\nprint(date) // nil\n```\n\nI've tried different formats, but the result is always nil. Can you tell me what I'm doing wrong? Maybe it's a locale issue? Or am I setting the format incorrectly? I would be very grateful for help, as this is urgent for my iOS project.\n\nAnother question: how can I then nicely display this date to the user in the format "15 October 2023"? Thanks in advance!