user12489593
user12489593

Reputation:

Converting date string to desired date format and the output is nil

I am trying to display date into tableview cell in the following format

1996.July.10 12:08 PM using this pattern yyyy.MMMM.dd hh:mm aaa

My actual date format from api is in this format 2019-12-16T05:33:43Z

Here is the code that I used for conversion

if let dateString: String = articleListVM.articles[indexPath.row].publishedAt {
            print(dateString)
            let dateFormatter = DateFormatter()
            dateFormatter.locale = Locale(identifier: "en_US_POSIX")
            dateFormatter.dateFormat = "yyyy.MMMM.dd hh:mm aaa"
            let date = dateFormatter.date(from: dateString)
            print("date: \(String(describing: date))")
        }

Right now I am getting the following output like this

2019-12-16T05:33:43Z
date: nil

I am not sure what mistake I have done that i am getting nil, please look into this.

Upvotes: 2

Views: 120

Answers (2)

Manikandan
Manikandan

Reputation: 1225

Please check following solution, I have tested its working fine

let inputdateString: String = "2019-12-16T05:33:43Z"
let dateFormatter  = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-mm-dd'T'hh:mm:ssZ"
let inputDate = dateFormatter.date(from: inputdateString )!
print("Input date: \(String(describing: inputDate))")
//Converting to Display Date, you can reuse same date formater 
dateFormatter.dateFormat = "yyyy.MMMM.dd hh:mm a"
let displayDate = dateFormatter.string(from: inputDate)
print("display date: \(String(describing: displayDate))")

output:-

Input date: 2019-01-16 05:33:43 +0000
display date: 2019.January.16 11:03 AM

Upvotes: 0

Martin
Martin

Reputation: 16433

You have a minor issue with the date format you are specifying. Your date format should actually be:

yyyy-MM-dd'T'HH:mm:ssZ

Therefore, change your code to:

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

Output:

date: Optional(2019-12-16 05:33:43 +0000)

Edit following further information from OP in comments

In order to convert from a string in one format to a date, and then to another date format, you need two DateFormatters. The following will convert the original string date and then output it the date to another format:

let dateString: String = "2019-12-16T05:33:43Z"
print(dateString)

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let date = dateFormatter.date(from: dateString)
{
  print("date: \(String(describing: date))")

  let outputFormatter = DateFormatter()
  outputFormatter.locale = Locale(identifier: "en_US_POSIX")
  outputFormatter.dateFormat = "yyyy.MMMM.dd hh:mm aaa"
  let outputDate = outputFormatter.string(from: date)
  print("date: \(String(describing: outputDate))")
}

Output:

2019-12-16T05:33:43Z
date: 2019-12-16 05:33:43 +0000
date: 2019.December.16 05:33 AM

Upvotes: 1

Related Questions