-3

I am trying to pass in a string and return it as a date. However when I pass the string in, the date returned is coming back as an UTC date. I tried the top solutions from this question but could not find the correct solution for Swift 4:

Swift convert string to date

I figured this was the best approach, but still returns in UTC:

func getDateFromDexReturnDateString(aDate : String) -> Date?
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        dateFormatter.timeZone = TimeZone.current  //Thought these two lines would solve it, but doen't
        dateFormatter.locale = Locale.current
        return dateFormatter.date(from: aDate)!
    }

Edit: Here is a picture of what is happening: Debugging Values

notice that the date that I pass in (aDate) is different than the date that is being returned (dateToReturn). Why wouldn't it be the same? I figured it was an UTC issue, but I may need to edit title of question. Thanks for any help.

BriOnH
  • 945
  • 7
  • 18
  • 2
    There's no such thing as a "UTC date". You are misunderstanding the output of viewing the resulting date. Please see http://www.maddysoft.com/articles/dates.html to better understand. – rmaddy Jun 20 '18 at 02:39

1 Answers1

0

BriOnH, do you mean that when print or log the returned date to the console or screen it is displayed in UTC date format? If you want a date's string representation other than UTC format you can use/call the instance method description(with locale: Locale?) with your wanted locale object. For a detailed explanation click on the link to check out the method's documentation.

user2760845
  • 124
  • 5
  • This would only be useful for debugging. Do not do this to display the date to a user. – rmaddy Jun 20 '18 at 03:03
  • When you want to display the date in your app you should display it in an appropriate format that is expected by your app's users. And you could do this either using Date Formatters or the Date's description(with locale: Locale?) method mentioned above. Check out this documentation https://developer.apple.com/documentation/foundation/date/1779759-description – user2760845 Jun 20 '18 at 03:17
  • 1
    Not my vote but no, do not use `description` to show the result to a user. Use `DateFormatter`. But none of this is relevant to the question. The OP simply doesn't understand the output of viewing a `Date` instance. – rmaddy Jun 20 '18 at 03:22
  • thanks for the attempt, sorry it got down voted for trying to help. I edited question. – BriOnH Jun 20 '18 at 20:49