-1
    var str = "2021-05-23T06:35:47.409Z"
    var formatter = DateFormatter()
    formatter.dateFormat = "MMM d yyyy, h:mm:ss a"
    let formattedtoDate = formatter.date(from: str)
    let formattedtoString = formatter.string(from: formattedtoDate) //Error Cannot force unwrap value of non-optional type 'String'
    cell.date_announce.text = formattedtoString

I'm trying to format sting to Date() and format Date to String in order to set value to date_announce label. Can anyone help me please?

greatkub
  • 3
  • 2
  • The comment in your code does not match your code. The error your code generates is "Value of optional type 'Date?' must be unwrapped to a value of type 'Date'" – Duncan C Jul 01 '21 at 17:00
  • Your date formatter does not match the format of your input date string. The initial `date(from:)` is going to fail. – Duncan C Jul 01 '21 at 17:02
  • Thank you Ducan and Joakim, I found this one and it solve my problem -> dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" – greatkub Jul 01 '21 at 17:29

1 Answers1

0

If you break down what you are trying to do, there are actually 2 steps that require two different date formatters.

  1. Convert an input date string (e.g. "2021-05-23T06:35:47.409Z") to a Date
  2. Convert the Date to an output string in a different format.

Try this code instead:

var str = "2021-05-23T06:35:47.409Z"

//Set up a DateFormatter for input dates in "Internet" date format"
var inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"

//Set up an output date formatter (in the local time zone)
var outputFormatter = DateFormatter()
outputFormatter.dateFormat = "MMM d yyyy, h:mm:ss a"

if let formattedtoDate = inputFormatter.date(from: str) {
    let formattedtoString = outputFormatter.string(from: formattedtoDate) 
    print("'\(str)' = '\(formattedtoString)'")
} else {
    print("Can't convert the string \(str) to a Date")
}

Edit:

(As Leo Dabus pointed out in a comment, you usually should not use a fixed dateFormat string in a DateFormatter that generates user-visible date strings. Better to use date and time styles and let the DateFormattter pick a specific format appropriate to the user's locale and language.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • OMG , This is so helpful. Thank you! – greatkub Jul 01 '21 at 17:32
  • if you don't set the locale to "en_US_POSIX" when parsing the date it might fail. The dateFormat will be affected by the device locale and settings, most specifically the 12-24h setting – Leo Dabus Jul 01 '21 at 18:40
  • Note that you should never use a fixed date format to display the date to the user. You should always use date and time style to display it respecting the user device locale and settings – Leo Dabus Jul 01 '21 at 18:42
  • I wouldn't go as far as to say "never", but yeah, it's usually better to let the DateFormatter convert the date based on styles. Hard-coded format strings don't adapt well to locale changes. – Duncan C Jul 01 '21 at 19:05