2

I am trying to get the current date in a time zone selected by the user, but I can't get anything other than the current date despite using functions from other answers on stackoverflow. What I used is this:

func dateIn(timeZone: TimeZone) -> Date {
let now = Date()
var calendar = Calendar.current
calendar.timeZone = timeZone
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)

let date = calendar.date(from: components) ?? Date()


   return date
}

I would really appreciate it if someone could help me understand why I'm not getting the date in the time zone although I changed the time zone of calendar to that time zone.

Aisha
  • 59
  • 8
  • `Date` has no timezone. You don't need any of the code you posted. Don't worry about any timezone until you wish to display the date to the user. Then use a DateFormatter with its timeZone property set as needed. – rmaddy Aug 05 '19 at 20:26
  • https://stackoverflow.com/a/28347285/2303865 – Leo Dabus Aug 05 '19 at 20:45

0 Answers0