0
extension Date {
    func toString() -> String {
        let dateFormatter = DateFormatter()
        
        dateFormatter.dateFormat = "MMMM dd, Y (E)"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
        
        return dateFormatter.string(from: self)
    }
}

extension String {
    func toDate() -> Date {
        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "MMMM dd, Y (E)"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")

        return dateFormatter.date(from: self)!
    }
}

That is my code. I want to convert my string value into a right date value. The results i’m getting are totally different form what i want.

Here is an example:

String : September 09, 2021 (Thu)
Converted Date : 2020-12-24 00:00:00 +0000

I want it to be like this:

Converted Date : September 09, 2021 (Thu) (or 2021-09-09 00:00:00 +0000)
PJH
  • 90
  • 9
  • Where does 'MMMM dd, Y (E)' come from? – El Tomato Sep 08 '21 at 03:55
  • @ElTomato that was just the format i want. date to string is working. but string to date isn't – PJH Sep 08 '21 at 04:01
  • 1
    Change `Y` to `y` or `yyyy`. [https://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter](https://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter) – Ricky Mo Sep 08 '21 at 04:35

3 Answers3

1

Can you change

dateFormatter.dateFormat = "MMMM dd, Y (E)"

To:

dateFormatter.dateFormat = "MMMM dd, yyyy (E)"

You pass the full year in String: September 09, 2021 (Thu) so you need to get yyyy to the right way.

extension String {
    func toDate() -> Date {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM dd, yyyy (E)"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
        return dateFormatter.date(from: self)!
    }
}

enter image description here

Amr
  • 286
  • 1
  • 2
  • 10
0

here you can do what you want:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy (E)" // here is your foramt
dateFormatter.timeZone = TimeZone(identifier: "UTC")
guard let date = dateFormatter.date(from: "September 09, 2021 (Thu)") else {
   fatalError()
}
print(date) // output: 2021-09-09 00:00:00 +0000

Note that, the time zone which you choosed, should matched to your device time zone.

0

Replace Your Function with Following Code

extension Date {
    func toString() -> String {
        let dateFormatter = DateFormatter()
        
        dateFormatter.dateFormat = "MMMM dd, yyyy (E)"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
        
        return dateFormatter.string(from: self)
    }
}

extension String {
    func toDate() -> Date {
        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "MMM dd,yyyy (E)"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")

        return dateFormatter.date(from: self)!
    }
}

There was slight variance of in your dateFormat in your toDate() Function

  • Replaced MMM from MMMM
  • Replaced yyyy from Y

I don't know why it was showing different date but now it should work

Nayan Dave
  • 1,078
  • 1
  • 8
  • 30