Reputation: 870
I am working on application where i want to get current date as per the users current timezone. even after user changes date from their device setting menu.
To get the current timezone i have used
let timeZone = TimeZone.current
print(timeZone)
for example here i got 'Asia/Kolkata' and current date is 28-july, now if user changes date (Setting->Date&Time-> off set automatically) to 29-july at that time i got output like 'Asia/Kolkata' date 29-july, but actual date is 28-july. so how can i get current date of this (Asia/Kolkata) timezone.
Note: I want to use this because i need current date of particular timezone if user tries to change date from setting then after i need to get exact date from that timezone.
Upvotes: 4
Views: 5705
Reputation: 236315
If you need to make sure the date is a valid date you can use a timeserver to return the date based on the device IP, of course this will require an internet connection.
You can create a asynchronous method to return the current date regardless of the user timezone and its timezone as well:
struct Root: Codable {
let unixtime: Date
let timezone: String
}
extension URL {
static let timeIP = URL(string: "http://worldtimeapi.org/api/ip")!
static func asyncTime(completion: @escaping ((Date?, TimeZone?, Error?)-> Void)) {
URLSession.shared.dataTask(with: .timeIP) { data, response, error in
guard let data = data else {
completion(nil, nil, error)
return
}
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
let root = try decoder.decode(Root.self, from: data)
completion(root.unixtime, TimeZone(identifier: root.timezone), nil)
} catch {
completion(nil, nil, error)
}
}.resume()
}
}
Usage:
URL.asyncTime { date, timezone, error in
guard let date = date, let timezone = timezone else {
print("Error:", error ?? "")
return
}
print("Date:", date.description(with: .current)) // "Date: Tuesday, July 28, 2020 at 4:27:36 AM Brasilia Standard Time\n"
print("Timezone:", timezone) // "Timezone: America/Sao_Paulo (current)\n"
}
Upvotes: 1
Reputation: 16341
Here is a simple solution:
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: Date()))
let date = Date(timeInterval: seconds, since: Date())
print(date)
You could simply use Date()
too, but while using it to display it use DateFormatter
. Here's an example:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let formattedDate = dateFormatter.string(from: Date())
print(formattedDate)
Upvotes: 5
Reputation: 130
If you want the current date of the user, you can simply implement let date = Date()
. It will return the date used on the users device.
This for example will be displayed for me
let date = Date() // Jul 28, 2020 at 8:22 AM
var timezone = TimeZone.current // Europe/Berlin (current)
Upvotes: -2