-1

Within my swift app I have an iso8601 date (2017-10-27T10:15:23Z) that I receive. I use the following for the DateFormatter dateFormat

yyyy-MM-dd'T'HH:mm:ssZ

I then am taking it and using another format to get just the hour/min.

h:mm a

The code is this

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let nsDate = formatter.date(from: date)
formatter.dateFormat = "h:mm a"
print(formatter.string(from: nsDate!))

The date outputs

5:15 not 10:15 like I expect it to be. I've tried using a different final formats but the results are the same. What am I doing wrong?

Micah Montoya
  • 757
  • 1
  • 8
  • 24
  • Please update your question with an actual value for `date` since `2017-10-27T10:15:23` is not a value that would be successfully parsed with the code you posted. – rmaddy Oct 27 '17 at 15:35
  • Fixed the typo. – Micah Montoya Oct 27 '17 at 15:37
  • You are parsing a string in the UTC timezone and your final result is in local time. So the results you see are correct for someone living 5 hours west of UTC (such as the Central timezone). – rmaddy Oct 27 '17 at 15:39
  • @rmaddy Wow. Totally forgot about local time from UTC. Thanks for the tip. Now I know what I need to adjust. – Micah Montoya Oct 27 '17 at 15:41

2 Answers2

1

5:15 not 10:15 like I expect it to be.

If I try it I get 4:15. Which points to the problem-- time zones. I'm six hours from UTC, and you are apparently five hours from UTC.

DateFormatter uses your local time zone unless you tell it to use something else. Since your original date string is UTC, you need to tell your date formatter to use UTC:

formatter.timeZone = TimeZone(identifier: "UTC")

Then you'll get "10:15".

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • This is not true. This won't make a difference when parsing the date. The formatter uses the dateFormat Z to interprete the timezone from the dateString. Setting the timezone to `TimeZone(identifier: "UTC")` will only affect the `string(from: Date)` output string that will be always displayed with UTC time. – Leo Dabus Oct 27 '17 at 16:37
  • And yet when I add that line, it **does** make a difference, exactly the difference I describe in the answer. – Tom Harrington Oct 27 '17 at 16:44
  • The date string including the Z means that the date it is UTC time. The dateFormat Z is used to interprete the timezone from the string. So setting the timezone it is not necessary unless you want the string from it to always be displayed at UTC time. I won't explain it again – Leo Dabus Oct 27 '17 at 16:48
  • Fine. I'll take "I tried it and it works" over "someone said it doesn't work". – Tom Harrington Oct 27 '17 at 16:49
  • @LeoDabus and Tom. You are both right. Tom - Leo's first comment is correct (just ignore the opening sentence of "This is not true"). He correctly states that setting the timezone here will ultimately only affect the output of `string(from:)` and not when parsing the string (since the date string and the first format specify a timezone) using `date(from:)`. So this answer does give the correct result assuming the OP want's the final result to be in UTC time. – rmaddy Oct 27 '17 at 17:14
-1

So if I understand the question correctly you are trying to convert time to 24 hours, right? Try changing

formatter.dateFormat = "h:mm a"

to:

formatter.dateFormat = "HH:mm "
Sami Ali
  • 163
  • 3
  • 10