I'am receiving following string: "2021-02-15T02:37:27.371243Z". I want to add hours by changing time zone of string to current time zone of device.
Date received from DateFormatter
with .current
timezone property from dateFormatter.date
method changes nothing.
Example:
let dateString = "2021-04-10T10:00:01.594119Z"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let date = dateFormatter.date(from: dateString)
let anotherDateFormatter = DateFormatter()
var calendar = Calendar.current
calendar.timeZone = .current
anotherDateFormatter.calendar = calendar
anotherDateFormatter.dateFormat = "hh:mm"
anotherDateFormatter.timeZone = .current
let string = anotherDateFormatter.string(from: date!)
print(string)
Output: 04:00. Incorrect, not my time.
I've come up with solution with calendar(current calendar has current time zone by default) by retrieving it's components:
let dateString = "2021-04-10T10:00:01.594119Z"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let date = dateFormatter.date(from: dateString)
print(hourWithMinutesForCurrentTimezone(date: date!))
private func hourWithMinutesForCurrentTimezone(date: Date) -> String {
let calendar = Calendar.current
let hours = String(calendar.component(.hour, from: date))
let formattedHours = hours.count == 1 ? "0\(hours)" : hours
let minutes = String(calendar.component(.minute, from: date))
let formattedMinutes = minutes.count == 1 ? "0\(minutes)" : minutes
return "\(formattedHours):\(formattedMinutes)"
}
Output: 16:00 - Yay! My time
It works and changes hour and minutes to the current timezone, but I don't find this solution elegant, anyone knows way of changing time to current timezone better?