0

I want convert this: Dec 31, 2017 8:00:00 PM to dd-mm-yyyy HH:ii format in Swift 4.

This is my code so far:

let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MMM dd, yyyy hh:mm:ss a"//this your string date format

    if let translatedDate = dateFormatter.date(from: datetime) {
        dateFormatter.dateFormat = "dd-MM-yyyy HH:ii"
        return dateFormatter.string(from: translatedDate)
    }

The code never enters in the if statement. I guess my date format is not correct.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
xhinoda
  • 1,032
  • 1
  • 19
  • 26
  • 1
    It seems that `translatedDate` is nil, I tried in playground, no issue. So, an issue with local? Else, I don't the the `ii` in the format (http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns), I don't know what you want to do with it, so if you pass the `if let` test, I don't think that `dateFormatter.string(from: translatedDate)` will be what you want. – Larme Dec 22 '17 at 13:03
  • 3
    Probably another "set `dateFormatter.locale = Locale(identifier: "en_US_POSIX")` " issue, compare https://stackoverflow.com/questions/40692378/dateformatter-doesnt-return-date-for-hhmmss or https://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feechur – but what does `ii` stand for? – Martin R Dec 22 '17 at 13:04
  • `8` will not match `hh`, because `hh` expects two digits (so `08`). Try a single `h` instead. – Cyrille Dec 22 '17 at 13:06
  • 1
    your code is fine: `let datetime = "Dec 31, 2017 8:00:00 PM" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MMM dd, yyyy hh:mm:ss a" dateFormatter.locale = Locale(identifier: "en_US_POSIX") if let translatedDate = dateFormatter.date(from: datetime) { dateFormatter.dateFormat = "dd-MM-yyyy HH:mm" let result = dateFormatter.string(from: translatedDate) print(result) // 31-12-2017 20:00 }` anyway, I think `ii` is a mistake – mugx Dec 22 '17 at 13:07
  • @Cyrille: No, `h` matches any hour from 1 to 12. – Martin R Dec 22 '17 at 13:11
  • thanks @AndreaMugnaini . !!! – xhinoda Dec 22 '17 at 13:25

1 Answers1

2

A DateFormatter that converts between dates and their textual representations.

You can find more Locale identifier here.

  • first, convert your date to local time zone. using a Locale class
  • after you can covert the date in specific formate.

And try this code.

let dateStr = "Wed, 26 Jul 2017 18:10:02 +0530"
if let date = Date(fromString: dateStr, format: "MMM dd, yyyy hh:mm:ss a") {
        debugPrint(date.toString(format: "dd-MM-yyyy HH:ii"))
}

Date Extension is ...

extension Date {

    // Initializes Date from string and format
    public init?(fromString string: String, format: String, identifier: String = Locale.current.identifier) {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.locale = Locale(identifier: identifier)
        if let date = formatter.date(from: string) {
            self = date
        } else {
            return nil
        }
    }

    // Converts Date to String, with format
    public func toString(format: String, identifier: String = Locale.current.identifier) -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: identifier)
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

String Extension is ...

extension String {
    // Converts String to formated date string, with inputFormat and outputFormat
    public func toDate(form inputFormat: String, to outputFormat: String, identifier: String = Locale.current.identifier) -> String? {
        return Date(fromString: self, format: inputFormat, identifier: identifier)?.toString(format: outputFormat, identifier: identifier)
    }

    // Converts String to Date, with format
    func toDate(format: String, identifier: String = Locale.current.identifier) -> Date? {
        return Date(fromString: self, format: format, identifier: identifier)
    }
}
AshvinGudaliya
  • 3,234
  • 19
  • 37
  • 4
    Force unwrapping the output of a dateformatter is a bad idea in a function which accepts the date format as an input argument. Either make your function throwable or return `nil` if either format is wrong. Moreover, if you are hardcoding the locale, set it to `en_US_POSIX`, not `en_US`. – Dávid Pásztor Dec 22 '17 at 13:07