0

I need to format this string date: "2019-01-18 10:14:52" to another string in format like this: 18 Jan 2019 - 10:14.

I use SwiftDate pod to format that string date using the code below:

import SwiftDate

func convert(date: String, toFormat: String) -> String {
    let userRegion = Region(calendar: Calendars.gregorian, zone: Zones.asiaJakarta, locale: Locales.indonesian)
    let convertedDate = date.toDate() ?? DateInRegion(Date(), region: userRegion)
    let userDateRegion = convertedDate.convertTo(region: userRegion)
    return userDateRegion.toFormat(toFormat, locale: Locales.indonesian)
}

But the time is always wrong. I expect to get 18 Jan 2019 - 10:14 but I always get 18 Jan 2019 - 17:14 in simulator.

It is always 7 hours more from the actual time. I suspect because I am in Indonesia now which is GMT+7. How to solve this issue?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • This would be trivial using `DateFormatter`. No need for a 3rd party library. – rmaddy Jan 18 '19 at 04:34
  • You should update your question showing the exact values being passed into this `convert` method. – rmaddy Jan 18 '19 at 04:35
  • rmaddy is correct. If you just need to convert the date's or string's format, no need for SwiftDate. Use that when you're dealing with lots of date stuff, such as when supporting multiple countries. Anyways, this could help you: https://nsdateformatter.com/ – Glenn Posadas Jan 23 '19 at 14:30

0 Answers0