-2

I am getting a date string from server in the format of "March 08, 2018 16:00:00 PST" where there is a lot of whitespace between Month & Date.

My intention is to basically remove those extra whitespaces. My idea is that- I will convert the string into Date object, and then convert back to string.

How can I convert this into Date object using Date Formatter, taking timezone into consideration.

I am concerned about the "PST" here. While converting the Date to String, I will need in the format - "March 08, 2018 16:00:00 PST" i.e. PST (or whatever time zone comes in) should stay intact in the final string.

Rashmi Ranjan mallick
  • 6,390
  • 8
  • 42
  • 59
  • Possible duplicate of [Swift convert string to date](https://stackoverflow.com/questions/36861732/swift-convert-string-to-date) – MwcsMac Mar 09 '18 at 14:00

3 Answers3

1
extension String {
    func getDate(fromFormat format: String = "MMMM dd, yyyy HH:mm:ss zzz") -> Date? {
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = format
      dateFormatter.locale =  Locale(identifier: "en_US_POSIX")
      return dateFormatter.date(from: self)
  }
}


let myDateString = "March 08, 2018 16:00:00 PST"
myDateString.getDate()

You can call with other time formats too.

Mansi
  • 628
  • 1
  • 8
  • 18
  • please, set locale to POSIX. That must be done for any parsing. For example, `March` is locale dependent. `HH` can be overwritten by `DateFormatter` to `hh`, year might be parsed using non-gregorian calendar and `PST` can be interpreted as `Philippine Standard Time` if you don't. – Sulthan Mar 09 '18 at 14:11
  • @Sulthan, Can you please post as answer? – Rashmi Ranjan mallick Mar 09 '18 at 14:13
  • Hey @Mansi, without setting timezone OR locale, it returns "March 09, 2018 05:30:00 GMT+5:30" – Rashmi Ranjan mallick Mar 09 '18 at 14:16
  • @RashmiRanjanmallick That's probably correct. The problems happen if you device language is not set to English or if you don't use gregorian calendar (e.g. if you are chinese). – Sulthan Mar 09 '18 at 14:17
  • @RashmiRanjanmallick That's a whole different question. You need a formatter set to `PST` time zone and format the given `Date` to `String`. Make sure you understand the difference between `Date` (a point in time, with no time zone) and a string representation of a date, relative to a time zone. – Sulthan Mar 09 '18 at 14:20
  • I got your point Sulthan! But, the point is server is now sending PST. If it sends some other time-zone in future, how should I make it generic? – Rashmi Ranjan mallick Mar 09 '18 at 14:23
-2

Try this

    let isoDate = "March 08, 2018 16:00:00 PST"
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMMM dd, yyyy HH:mm:ss z"
    let date = dateFormatter.date(from: isoDate)!
Manish Malviya
  • 546
  • 2
  • 15
  • 1
    For one thing, you *have* to set the locale to POSIX. Parsing just won't work correctly if you don't. Also, please use `DateFormatter`, not `NSDateFormatter` in Swift. Also `Z` has to be lowercase `z`. – Sulthan Mar 09 '18 at 14:08
  • oh sorry basically mostly I used to code in objective C so got that error. I have updated the answer thank you – Manish Malviya Mar 09 '18 at 14:12
-2

Try this

class  func stringToDate (dateString:String, dateFormat:String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = dateFormat
    let dateFromString = dateFormatter.date(from: dateString)
    return dateFromString
}
SAURABH SANWAL
  • 139
  • 1
  • 11
  • This is the same answer as above. How does it handle the 'PST' part? – koen Mar 09 '18 at 14:23
  • Check this , Hope this works - [How can I convert including timezone date in swift?](https://stackoverflow.com/questions/32022906/how-can-i-convert-including-timezone-date-in-swift/32023076#32023076) – SAURABH SANWAL Mar 09 '18 at 14:32